'Iteration of List<String> with modyfing String
I can't modyfing 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.
Solution 1:[1]
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());
}
Solution 2:[2]
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.
Solution 3:[3]
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);
}
Solution 4:[4]
Java strings are immutable, hence they cannot be modified. Further, if you wish to modify a list use the iterator interface.
Solution 5:[5]
As others have pointed out:
- You can't modify strings in Java, so
s = "x" + swill create a new string (which will not be contained in the list) - Even if you could, the variable
sis 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());
Solution 6:[6]
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.
Solution 7:[7]
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")).
Solution 8:[8]
Another option, use replaceAll()
List<String> stringList = new ArrayList<>(List.of(myStrings));
serialsList.replaceAll(s -> "x" + s);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | tenshi |
| Solution 3 | Sanjay T. Sharma |
| Solution 4 | Johan Sjöberg |
| Solution 5 | |
| Solution 6 | Björn |
| Solution 7 | Carl |
| Solution 8 | ptan9o |
