Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Collection Error

 
pankaj patil
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Vlado Zajac
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15203
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
pankaj patil
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

thanx for reply

with regards
pankaj
 
Aadi Narayana Reddy
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Adam Richards
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry to hear were executed. What did you do to make someone mad?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic