aspose file tools*
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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Collection Error" Watch "Collection Error" New topic
Author

Collection Error

pankaj patil
Ranch Hand

Joined: Dec 19, 2004
Posts: 98
Hi

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)
{
Levelobject[i].removeQid(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(qid);
while(itr.hasNext())
{
System.out.print("\npankaj \n");
//if (convert((String)i.next())==qid)
//{
//Object element= itr.next();
System.out.print(itr.next()+"\n");
//i.remove();
//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();
s.initialise(2);
s.addQid(0,5);
s.addQid(0,6);
s.addQid(0,7);

s.addQid(1,1);
s.addQid(1,2);
s.addQid(1,3);

System.out.print(s.getList(0)+"\n");
System.out.print(s.getList(1));
s.removeQid(0,2);
System.out.print(s.getList(1));


}

}




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


Regards,
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: 14269
    
  21

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
hi

thanx for reply

with regards
pankaj
Aadi Narayana Reddy
Greenhorn

Joined: Oct 17, 2005
Posts: 13
hi,

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

regards,
aadi


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?
 
 
subject: Collection Error