简体   繁体   English

我想用另一个字符替换字符串arraylist每个元素的第四个字符

[英]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: 您可以这样做:

  • Define a counter variable to be index of element in list3 starting 0. 将计数器变量定义为从0开始的list3中元素的索引。
  • Iterate over each element in the list2 遍历list2中的每个元素
  • Remove the string given by the iterator and convert it to charArray using toCharArray api on String 删除迭代器给出的字符串,并使用String上的toCharArray api将其转换为charArray
  • now change the bit using 4th index in char array using list3's element pointed by counter. 现在使用counter指向的list3元素,使用char数组中的第4个索引更改该位。
  • Convert char array back to String (using String(char[]) constructor) and add it to the list 2. 将char数组转换回String(使用String(char [])构造函数)并将其添加到列表2。
  • increment the counter. 递增计数器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java-如果字符串在ArrayList中,如何查看字符串中的第四个元素? - java - How can I look at the fourth element in a string if the strings are in an ArrayList? 检查ArrayList中的每个字母 <String> 是某个角色? - Check if each letter in ArrayList<String> is a certain character? 我要为输入字符串的用户中的每个字符显示字母“ z” - I want to display letter 'z' for each character in a String user enters 将“\\”替换为使用String的replace()的任何其他字符 - Replace “\'” with any other character with String's replace() Java ArrayList<string> , 获取元素 0 中的一个字符</string> - Java ArrayList<String>, getting a character in element 0 用字符串中的其他特殊字符替换特殊字符 - Replace a special character with other special character within string 我想将字符串中每次出现模式后的字符添加到字符数组列表中 - I want to add the character after every occurrence of a pattern in a string to an arraylist of characters 字符串压缩为字符 ArrayList - String Compression to Character ArrayList 如何在ArrayList中大写每个元素的第一个字符并删除? - How to capitalize the first character of each element AND delete in an ArrayList? 我如何拆分 ArrayList<String> 到数组列表<Character>在java中? - How can i split ArrayList<String> to ArrayList<Character> in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM