java.util.ConcurrentModificationException
Hi,
I wrote the following code and try to execute that code I throws the following exception "java.util.ConCurrentModificationException" .I want to know what happened there and why it throws a exception
The code is following:
package com;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;
public class ArrayListDemo {
/**
* @param args
*/
public static void main(String[] args) {
ArrayList<Integer> i=new ArrayList<Integer>();
i.add(1);
i.add(2);
i.add(5);
System.out.println("The arraylist is"+i);
Iterator<Integer> it=i.iterator();
while(it.hasNext()){
System.out.println("The values of i is"+it.next());
}
/*
* Using List Iterator
*/
ListIterator lit=i.listIterator();
while(lit.hasNext()){
System.out.println(lit.next());
Object obj=((Integer)lit.next()).intValue();
if(obj.equals(i.get(1))){
i.add(5);
i.set(0,100);
}
}
System.out.println(i);
}
}
It gives the Following output:
The arraylist is[1, 2, 5]
The values of i is1
The values of i is2
The values of i is5
1
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372)
at java.util.AbstractList$Itr.next(AbstractList.java:343)
at com.ArrayListDemo.main(ArrayListDemo.java:36)
please say me why it throws the exception . Is i did anything wrong in my code.
Thanks in Advance,
Maheshwaran Devaraj

