0
因此,我们有以下几点:修改字符串的ArrayList中的特定字符
ArrayList<String> list = (new ArrayList<String>());
list.add(new String("hello")); // add first word at position [0]
list.add(new String("world")); // add second word at position [1]
我不想使用.replace方法,将取代所有出现。假设我只想修改hello单词中的第一个“l”并将其更改为“x”,那我该怎么做?我只想定位数组中字符串中的特定元素#。
System.out.println(list); // display the full arraylist
初始输出:
[hello, world]
所需的输出:
[hexlo, world]
我们可以看看你试过的代码是什么? –
不要使用'new String()'作为字符串文字。你无需创建额外的对象。 – shmosel
您可以使用'replaceFirst()'方法或使用'StringBuilder'而不是'String'并使用'setCharAt()'方法。 –