[英]i want replace fourth character of each element of the string arraylist with the other character
list2.get(i).replace(list2.get(i).charAt(4), list3.get(i));
list2
is the string arraylist, for example [00110001, 11111111, 00000000, 01001010, 00000000]
list2
是字符串[00110001, 11111111, 00000000, 01001010, 00000000]
例如[00110001, 11111111, 00000000, 01001010, 00000000]
I want to replace 4th character of each element in list2, that replacing character store in list3
so it will contain [1, 0, 1, 1, 1]
我想替换list2中每个元素的第4个字符,即替换
list3
字符存储,以便它包含[1, 0, 1, 1, 1]
list3
[1, 0, 1, 1, 1]
Finally my arraylist list2 should look like [00111001, 11110111, 00001000, 01001010, 00001000]
. 最后,我的arraylist list2应该看起来像
[00111001, 11110111, 00001000, 01001010, 00001000]
。
First, you have to note that String
is immutable. 首先,您必须注意
String
是不可变的。 This means that you can't do this just by running any method on list2.get(i)
. 这意味着您不能仅通过在
list2.get(i)
上运行任何方法来执行此操作。 All string methods return new string values, but do not change the values of the original string. 所有字符串方法都返回新的字符串值,但不更改原始字符串的值。
So you have to use list2.set( i, newValue )
to replace the previous string with a new string that you will construct. 因此,您必须使用
list2.set( i, newValue )
将先前的字符串替换为将要构造的新字符串。
Now, how do you construct value? 现在,您如何构建价值? You take the parts of the string that are not supposed to change, and stick the new character value between them:
您将不应该更改的字符串部分放入新的字符值之间:
Something like: 就像是:
String oldValue = list2.get(i);
String newValue = oldValue.substring(0,3) + list3.get(i) + oldValue.substring(5);
You could do it something like: 您可以这样做:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.