Issue
I am in a very peculiar state. I have a list something like below :-
List<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
Now when i do multiple type of traversing, like using advanced for, iterator, and normal for loop, below are the sample code snippets :-
1> Advanced Loop :-
try {
for(String a : list) {
System.out.println(a);
list.add("f");
}
} catch (Exception e) {
e.printStackTrace();
}
2> Iterator :-
try {
Iterator<String> itr = list.iterator();
while(itr.hasNext()) {
System.out.println(itr.next());
list.add("f");
}
} catch (Exception e) {
e.printStackTrace();
}
3> Normal Loop :-
for (int i=0;i<list.size();i++) {
System.out.println(list.get(i));
list.add("f");
}
Now, the peculiar problem is, that when using advanced for-loop and iterator, i get the following exception : -
java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
at java.util.ArrayList$Itr.next(Unknown Source)
the reason i know, that while iterating through a list, one cannot modify it parallely.
but when i use the normal for loop, then it works properly, Am i missing something??
Please Help!!!..
Solution
If you modify a List, it invalidates any Iterator objects created from it. The advanced loop (1) compiles to nearly the same code as the iterator loop (2), meaning that there is an Iterator object created behind the scenes.
The javadocs for ConcurrentMOdificationException have more details.
If you want to add while iterating, use
ListIterator listIter = ...
while(listIter.hasNext())
{
if(shouldAdd(iter.next())) {
iter.add(....)
}
}
Answered By - sbridges
Answer Checked By - Terry (JavaFixing Volunteer)