This question already has an answer here:
- Where to find Java JDK Source Code? [closed] 11 answers
- Where can I locate and browse JDK 7 source files? [closed] 8 answers
- Where can I find Java 8 src.zip [closed] 2 answers
- Where can I see the source code of the Sun JDK? 10 answers
- Where can I find the JDK 8/JavaFX 8 source code? [closed] 4 answers
I have the following code which throws ConcurrentModificationException because I am using two different iterators on the same list and one of them is modifying the list. So, the second iterator throws the exception when reading the list because some other iterator has modified the list.
List<Integer> list = new ArrayList<>();
populate(list);//A method that adds integers to list
ListIterator<Integer> iterator1 = list.listIterator();
ListIterator<Integer> iterator2 = list.listIterator();
while (iterator1.hasNext()) {
if(iterator1.next() < 5)
iterator1.remove();
}
while (iterator2.hasNext()){
if(iterator2.next() < 5) {
//Call handler
}
}
My question is how does iterator2
know internally that the list
has has been modified by some other iterator if it has not reached an element which is yet removed by iterator1
? How does it figure out that some other iterator
has mutated the list
? One way could be keep track of size but that can't be the reason since some other iterator can just replace any element.