Before your eyes look back at another ConcurrentModificationException question, this is not your typical ConcurrentModificationException question.
I understand ConcurrentModificationExceptions, but I do not understand why I get it in the following code. The "for" below shows that the iterator continues to live outside the for loop. (Using the debugger in eclipse, I see that the iterator preserves the jList modCount value when the loop restarts.)
public class ForEachTest {
public static void main(String[] args) {
List<Integer> zList = new LinkedList<Integer>();
List<Integer> jList = new LinkedList<Integer>();
jList.add(1);
while (true) {
for (Integer j : jList) {
zList.add(17);
System.out.println(j);
}
jList = zList;
}
}
}
At first I thought it might be a syntactic sugar problem, but I rewrote it with an explicit iterator and came across the same behavior.
, , , , . , , . for-each, .