my dog learned polymorphism
The moose likes Java in General and the fly likes Collection Error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Collection Error" Watch "Collection Error" New topic

Collection Error

pankaj patil
Ranch Hand

Joined: Dec 19, 2004
Posts: 98

when i was executed this program i got this error . Can any one Help me

with Regards

import java.util.*;

class SectionLevel
private level Levelobject[];

public void initialise(int i)
Levelobject = new level[i]; // reference are created
for(int j = 0; j < i; j++)
Levelobject[j] = new level();

public void addQid(int i,int qid)
Levelobject[i].addQid(qid);// call the addQid for the level i

public int getQid(int i,int j)

return Levelobject[i].getQid(j);
// i represent the levelobject and j represent the jth qid in the list

public List getList(int i)
return (List)Levelobject[i].getList();
public void removeQid(int i,int qid)

}// end of sectionlevel

// class which store the qid
class level

private ArrayList qid=new ArrayList();
private Iterator itr=qid.iterator();

public void addQid(int val)
qid.add(val+""); // add the qid in list

public int getQid(int j)
return convert((String)qid.get(j));

public List getList()
return qid;

public synchronized void removeQid(int qid)
System.out.print("\npankaj \n");
//if (convert((String)
//Object element=;
//break loop;

}// end of removeqid

public int convert(String v)
return Integer.valueOf(v).intValue();


class Main
public static void main(String arg[])
SectionLevel s=new SectionLevel();





---------- java ----------
[5, 6, 7]
[1, 2, 3]2
at java.util.AbstractList$Itr.checkForComodification(
at java.util.AbstractList$
at level.removeQid(
at SectionLevel.removeQid(
at Main.main(
Exception in thread "main"
Output completed (0 sec consumed) - Normal Termination

Pankaj Patil
Vlado Zajac
Ranch Hand

Joined: Aug 03, 2004
Posts: 245
You cannot use an iterator for a collection and modify the collection in the same time (except for modifications done via that iterator). Class level does this.

The iterator itr is created for the ArrayList qid when level object is created, then you add something to list qid. After that the iterator is useless.

It seems that itr is only used in removeQid method, so you can make itr local to that method:

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15081

Did you lookup the API documentation of java.util.ConcurrentModificationException? It explains in detail what the error message means. A quote:

This exception may be thrown by methods that have detected concurrent modification of an object when such modification is not permissible.

For example, it is not generally permssible for one thread to modify a Collection while another thread is iterating over it. In general, the results of the iteration are undefined under these circumstances. Some Iterator implementations (including those of all the collection implementations provided by the JRE) may choose to throw this exception if this behavior is detected. 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.

Note that this exception does not always indicate that an object has been concurrently modified by a different thread. If a single thread issues a sequence of method invocations that violates the contract of an object, the object may throw this exception. For example, if a thread modifies a collection directly while it is iterating over the collection with a fail-fast iterator, the iterator will thow this exception.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
pankaj patil
Ranch Hand

Joined: Dec 19, 2004
Posts: 98

thanx for reply

with regards
Aadi Narayana Reddy

Joined: Oct 17, 2005
Posts: 13

as for as my knowledge goes, The arrayList is not a synchronized one.
So concurrent operations on the object of the arraylist may result in exceptions. So u can use the synchronized objects like Vector, Hashtable.
or u can get the synchronized version of the arraylist object by the methods provided in java util package


G.Adi Narayana Reddy,
Adam Richards
Ranch Hand

Joined: Nov 03, 2005
Posts: 135
Sorry to hear were executed. What did you do to make someone mad?
I agree. Here's the link:
subject: Collection Error
It's not a secret anymore!