Iteration of List<String> with modyfing String
I can't modyfin开发者_如何学JAVAg element of List this way:
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.
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:
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());
}
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.
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);
}
Java strings are immutable, hence they cannot be modified. Further, if you wish to modify a list use the iterator interface.
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.
The solution is in this case to use a StringBuilder
which represents a string which you can actually modify, or to use a ListIterator
as @Michael Borgwardt and @jarnbjo points out.
Using a 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
:
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());
ideone.com demo
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.
You can't modify a String
element of a List
that way, but a StringBuilder
would work just fine:
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")
).
Another option, use replaceAll()
List<String> stringList = new ArrayList<>(List.of(myStrings));
serialsList.replaceAll(s -> "x" + s);
精彩评论