I am trying to add an String object into ArrayList<String>
while iterating it. then i have a Exception like :
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:859)
at java.util.ArrayList$Itr.next(ArrayList.java:831)
at com.alonegk.corejava.collections.list.ArrayListDemo.main(ArrayListDemo.java:19)
the piece of code as -
public static void main(String[] args) {
ArrayList<String> al =new ArrayList<String>();
al.add("str1");
al.add("str2");
Iterator<String> it = al.iterator();
while(it.hasNext()){
System.out.println(it.next());
al.add("gkgk");
}
there is no synchronization here. i need to know the cause of this exception ?