I just read that concurrent modification exception would occur if we add, remove,or update collection after calling iterator method
I understand why adding and removing a collection element would cause concurrent modification exception, but why updating should cause concurrent modification? after all we dont change anything structurally while updating an element.
for example the below code is from arraylist implementation
public E set(int index, E element) {
rangeCheck(index);
E oldValue = elementData(index);
elementData[index] = element;
return oldValue;
}
we dont update the variable "modcount" which is actually used for checking concurrent modification.
i also tried with my custom code:
public static void main(String[] args) {
ArrayList l= new ArrayList();
l.add("string");
l.add(3);
Iterator it=l.iterator();
Object o=it.next();
l.set(0, "element");
l.remove(o);
//l.add(7);
//it.next();
System.out.println(it.next());
System.out.println(l.get(0));
int i;
System.out.println(j+" "+j);
}
this doesn't throw concurrent modifcation exception either.
may i know why?