satory
asked on
Problem with ConcurrentModificationException while using a vector
Im using a vector to hold a grouping of game objects. each of those objects have an alive boolean. What I want to do is loop though the vector and remove the objects from the vector once the alive boolean is set to false.
Now the first part of the code is where the vector is initialized, in the case of what I'm doing it can be any size, and the second part is where I'm getting my ConcurrentModificationExce ption on the for loop. From various reading around I found out that that exception had something to do with the finding the next element, and that why I'm using clear(), once to size of the vector is 1, rather than remove()
Heres my stacktrace if it helps.... (line 155 is the foreach loop in the second code section)
Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModifi cationExce ption
at java.util.AbstractList$Itr .checkForC omodificat ion(Unknow n Source)
at java.util.AbstractList$Itr .next(Unkn own Source)
at ie.gmit.game.GameModel.che ckEndGame( GameModel. java:155)
at ie.gmit.game.GameModel$Tim eCounterLi stener.act ionPerform ed(GameMod el.java:30 1)
at javax.swing.Timer.fireActi onPerforme d(Unknown Source)
at javax.swing.Timer$DoPostEv ent.run(Un known Source)
at java.awt.event.InvocationE vent.dispa tch(Unknow n Source)
at java.awt.EventQueue.dispat chEvent(Un known Source)
at java.awt.EventDispatchThre ad.pumpOne EventForFi lters(Unkn own Source)
at java.awt.EventDispatchThre ad.pumpEve ntsForFilt er(Unknown Source)
at java.awt.EventDispatchThre ad.pumpEve ntsForHier archy(Unkn own Source)
at java.awt.EventDispatchThre ad.pumpEve nts(Unknow n Source)
at java.awt.EventDispatchThre ad.pumpEve nts(Unknow n Source)
at java.awt.EventDispatchThre ad.run(Unk nown Source)
Now the first part of the code is where the vector is initialized, in the case of what I'm doing it can be any size, and the second part is where I'm getting my ConcurrentModificationExce
Heres my stacktrace if it helps.... (line 155 is the foreach loop in the second code section)
Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModifi
at java.util.AbstractList$Itr
at java.util.AbstractList$Itr
at ie.gmit.game.GameModel.che
at ie.gmit.game.GameModel$Tim
at javax.swing.Timer.fireActi
at javax.swing.Timer$DoPostEv
at java.awt.event.InvocationE
at java.awt.EventQueue.dispat
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre
M_men = new Vector<Merchantmen>();
synchronized (M_men){
for (int i = 0; i < MAX_NUM_MERCHANT; i++) {
Merchantmen m = new Merchantmen();
M_men.add(m);
}
}
----------------------------------------------------------------
for(Merchantmen m: M_men){
if(m.isAlive() == false)
{
if(M_men.size()==1){
M_men.clear();
}else{
M_men.remove(m);
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Okay I have changed the code to the following, but im still getting the same error, mainly when it gets down to the last one.
Also I just noticed that the size of the vector M_men doesn't seem to decrement when I say M_men.remove(i);
Also I just noticed that the size of the vector M_men doesn't seem to decrement when I say M_men.remove(i);
Iterator i = M_men.iterator();
while (i.hasNext())
{
Merchantmen m = (Merchantmen)i.next();
if (m.isAlive()==false)
{
M_men.remove(i);
}
}
> M_men.remove(i);
should be:
i.remove();
should be:
i.remove();
ASKER
cheers...
i should of gone back to look at the first response after i found out how to use an iterator
i should of gone back to look at the first response after i found out how to use an iterator
>>M_men.remove(i);
>>Use an Iterator (i) and call i.remove on it
>>Use an Iterator (i) and call i.remove on it
:-)
> I'm using clear(), once to size of the vector is 1, rather than remove()
No its to do with Iterators being fail-fast. Basically you can't call next() after mofiying the contents. Its discussed in the javadoc if you are interested.