java 使用修改字符串迭代 List<String>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4851663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Iteration of List<String> with modyfing String
提问by j2j
I can't modyfing element of List this way:
我不能以这种方式修改 List 的元素:
for (String s : list)
{
s = "x" + s;
}
After execution this code elements of this list are unchanged How to achieve iteration with modyfing through List in the simplest way.
执行后这个list的代码元素不变如何通过List以最简单的方式实现modyfing迭代。
回答by Gabe
Since String
objects are immutable, you cannot change the values you're iterating over. Furthermore, you cannot modify the list you're iterating over in such a loop. The only way to do this is to iterate over the list indexes with a standard loop or to use the ListIterator
interface:
由于String
对象是不可变的,因此您无法更改正在迭代的值。此外,您不能修改在这样的循环中迭代的列表。唯一的方法是使用标准循环迭代列表索引或使用ListIterator
接口:
for (int i = 0; i < list.size(); i++)
{
list.set(i, "x" + list.get(i));
}
for (ListIterator i = list.listIterator(); i.hasNext(); )
{
i.set("x" + i.next());
}
回答by tenshi
Strings are immutable beasts, so I can recommend to follow this philosophy and create new list instead modifying one:
字符串是不可变的野兽,所以我建议遵循这一理念并创建新列表而不是修改一个:
List<String> mappedList = new ArrayList<String>();
for (String s : list) {
mappedList.add("x" + s);
}
I believe, that this will make your code easier to understand and maintain.
我相信,这将使您的代码更易于理解和维护。
回答by Sanjay T. Sharma
Something like this should do the job:
这样的事情应该可以完成这项工作:
public static void main(String[] args) throws Exception {
final List<String> lst = Arrays.asList("a", "b", "c");
for(final ListIterator<String> iter = lst.listIterator(); iter.hasNext(); ) {
final String s = iter.next();
iter.set(s + "x");
}
System.out.println(lst);
}
回答by aioobe
As others have pointed out:
正如其他人指出的那样:
- You can't modify strings in Java, so
s = "x" + s
will create a new string (which will not be contained in the list) - Even if you could, the variable
s
is a local variables, which, when assigned to, does not affect the values contained in the list.
- 你不能在 Java 中修改字符串,所以
s = "x" + s
会创建一个新的字符串(它不会包含在列表中) - 即使可以,该变量
s
也是一个局部变量,当分配给它时,不会影响列表中包含的值。
The solution is in this case to use a StringBuilder
which represents a string which you canactually modify, or to use a ListIterator
as @Michael Borgwardt and @jarnbjo points out.
在这种情况下,解决方案是使用 aStringBuilder
表示您可以实际修改的字符串,或者使用 aListIterator
作为@Michael Borgwardt 和@jarnbjo 指出的。
Using a StringBuilder
:
使用StringBuilder
:
List<StringBuilder> someStrings = new LinkedList<StringBuilder>();
someStrings.add(new StringBuilder("hello"));
someStrings.add(new StringBuilder("world"));
for (StringBuilder s : someStrings)
s.insert(0, "x");
Using a ListIterator
:
使用ListIterator
:
List<String> someStrings = new LinkedList<String>();
someStrings.add("hello");
someStrings.add("world");
for (ListIterator<String> iter = someStrings.listIterator(); iter.hasNext();)
iter.set("x" + iter.next());
回答by Johan Sj?berg
Java strings are immutable, hence they cannot be modified. Further, if you wish to modify a list use the iterator interface.
Java 字符串是不可变的,因此它们不能被修改。此外,如果您希望修改列表,请使用迭代器接口。
回答by Bj?rn
In your loop you're just modifying the local copy of the String. A better alternative would be to use the iterator of the list, and replace the current position of the list.
在您的循环中,您只是在修改字符串的本地副本。更好的选择是使用列表的迭代器,并替换列表的当前位置。
Edit, Oops, way to slow.
编辑,哎呀,慢的方式。
回答by Carl
You can't modify a String
element of a List
that way, but a StringBuilder
would work just fine:
您不能String
以List
这种方式修改 a 的元素,但是 aStringBuilder
可以正常工作:
for (StringBuilder sb : list) sb.append("x");
The same is true for other primitive vs reference situations and the for-each loop. In the loop, the Iterable
is immutable, but the state of items in it is not - primitives (like String
) do not have state and hence you're only modifying a local copy, but references can have state and hence you can mutate them via any mutator methods they might have (e.g., sb.append("x")
).
对于其他原始与参考情况以及 for-each 循环也是如此。在循环中,Iterable
是不可变的,但其中项目的状态不是 - 原语(如String
)没有状态,因此您只是在修改本地副本,但引用可以有状态,因此您可以通过任何方式改变它们他们可能拥有的mutator方法(例如,sb.append("x")
)。