The following code does not throw ConcurrentModificationException
public static void main(String[] args)
{
List<String> l1 = new ArrayList<>();
l1.add("v1");
Iterator<String> it = l1.iterator();
l1.remove(0);
while(it.hasNext()) {
System.out.println(it.next());
}
}
Whereas this code throws ConcurrentModificationException
public static void main(String[] args)
{
List<String> l1 = new ArrayList<>();
l1.add("v1");
Iterator<String> it = l1.iterator();
l1.add("v2");
while(it.hasNext()) {
System.out.println(it.next());
}
}
Both operations are structural modifications of list, but why exception is thrown only in case of addition?