I understand that in Java a Collection<E>
should not be modified while iterating through it, such as removing or adding elements. But what about changing the elements in a List? For example, what if we have
List<String> letters = new ArrayList<String>(); letters.add("A"); letters.add("B"); letters.add("C"); int i = 0; for (String letter : letters) { letters.set(i, "D"); i++; }
So, I'm not talking about modifying the object stored at an element; I'm talking about changing what the object is. The size of the List is not being changed, but the object at the index is changing, so technically the List is being modified. My boss claims this code is fine (and it does appear to work), but I still don't think it is correct. Would a different way of doing it, maybe using the set(E e) method of a ListIterator, be better?
The Best Answer is At the end the whole list will have the letter "D" as its content. It's not a good idea to use an enhanced for loop in this case, you're not using the iteration variable for anything, and besides you can't modify the list's contents using the iteration variable.
The general rule of thumb is that you don't modify a collection/array/list while iterating over it. Use a secondary list to store the items you want to act upon and execute that logic in a loop after your initial loop.
You can't modify a Collection while iterating over it using an Iterator , except for Iterator.
The size of the List is not being changed, but the object at the index is changing, so technically the List is being modified.
There is nothing wrong with the idea of modifying an element inside a list while traversing it (don't modify the list itself, that's not recommended), but it can be better expressed like this:
for (int i = 0; i < letters.size(); i++) { letters.set(i, "D"); }
At the end the whole list will have the letter "D"
as its content. It's not a good idea to use an enhanced for
loop in this case, you're not using the iteration variable for anything, and besides you can't modify the list's contents using the iteration variable.
Notice that the above snippet is not modifying the list's structure - meaning: no elements are added or removed and the lists' size remains constant. Simply replacing one element by another doesn't count as a structural modification. Here's the link to the documentation quoted by @ZouZou in the comments, it states that:
A structural modification is any operation that adds or deletes one or more elements, or explicitly resizes the backing array; merely setting the value of an element is not a structural modification
Use CopyOnWriteArrayList
and if you want to remove it, do the following:
for (Iterator<String> it = userList.iterator(); it.hasNext() ;) { if (wordsToRemove.contains(word)) { it.remove(); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With