Ramakrishna Udupa wrote:
My code is like this,
Basically, you are not allowed to change the list (without using the iterator) while you are iterating. If you want to change the list while iterating, you need to do it with the iterator (and not with the list).
Ramakrishna Udupa wrote:
Its a simple program. I've list with some names as values like,
.
Now, I want to add "Hello" to each name by running above code. While iterating, first time I get Ram, I want to add "Hello" to Ram and add to list as "Ram Hello" also I want to remove original Value i.e "Ram". When I run above code it is giving Concurrent Modification Exception. How to do this?
Instead of using the add() and remove() methods, perhaps using the set() method (which does replacement) is a better option. Also, it would be a good idea to use a java.util.ListIterator instead, as that iterator as the methods that you need.
[EDIT: and I got beaten to the answer by three minutes]
Henry