java.util.ConCurrentModificationException

I tried to execute following code

ListIterator iterator = i.listIterator();
while(iterator.hasNext()) {
      System.out.println(iterator.next());
      Object obj=((Integer)iterator.next()).intValue();
      if(obj.equals(i.get(1))) {
           i.add(10);
      }
}

ant it throws following exception

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)

Related Post

No related posts

Comments

One Response to “java.util.ConCurrentModificationException”

  1. Shaili on August 3rd, 2009 10:57 am

    ConcurrentModificationException is thrown when one thread tries to modify a Collection while another thread is iterating over it. In general, the results of the iteration are undefined under such circumstances. Some Iterator implementations may choose to throw this exception if this behavior is detected while others can. Iterators that do this are known as fail-fast iterators, as they fail quickly and cleanly, rather that risking arbitrary, non-deterministic behavior at an undetermined time in the future.
    In above case we are modifying collection in following line
    i.add(10);
    at the same time we are iterating the collection.
    That is the reason for ConcurrentModificationException.

Leave a Reply




Technology