import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;
public class MyList {
public static void main(String[] args) {
ArrayList<String> al = new ArrayList<String>();
al.add("S1");
al.add("S2");
al.add("S3");
al.add("S4");
Iterator<String> lir = al.iterator();
while (lir.hasNext()) {
System.out.println(lir.next());
}
al.add(2, "inserted");
while (lir.hasNext()) {
System.out.println(lir.next());
}
}
}
The particular piece of code throws an error:
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
at java.util.ArrayList$Itr.next(Unknown Source)
at collections.MyList.main(MyList.java:32)
How do you fix Java's ConcurrentModificationException? There are two basic approaches: Do not make any changes to a collection while an Iterator loops through it. If you can't stop the underlying collection from being modified during iteration, create a clone of the target data structure and iterate through the clone.
What Causes ConcurrentModificationException. The ConcurrentModificationException generally occurs when working with Java Collections. The Collection classes in Java are very fail-fast and if they are attempted to be modified while a thread is iterating over it, a ConcurrentModificationException is thrown.
It happens due to array list is modified after creation of Iterator
.
The iterators returned by this ArrayList's iterator and listIterator methods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.
Documentation
Iterator<String> lir = al.iterator(); // Iterator created
while (lir.hasNext())
System.out.println(lir.next());
al.add(2, "inserted"); // List is modified here
while (lir.hasNext())
System.out.println(lir.next());// Again it try to access list
What you should do here create new iterator object after modification.
...
al.add(2, "inserted");
lir = al.iterator();
while (lir.hasNext())
System.out.println(lir.next());
You are modifying the Collection and then trying to use the same iterator.
Get the Collection iterator again
al.add(2, "inserted");
Iterator<String> lirNew = al.iterator();
while (lirNew.hasNext()) {
System.out.println(lirNew.next());
}
or Use ListIterator
ArrayList<String> al = new ArrayList<String>();
al.add("S1");
al.add("S2");
al.add("S3");
al.add("S4");
ListIterator<String> lir = al.listIterator();
while (lir.hasNext()) {
System.out.println(lir.next());
}
lir.add("insert");
while (lir.hasNext()) {
System.out.println(lir.next());
}
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