Link to home
Start Free TrialLog in
Avatar of satory
satoryFlag for Ireland

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 ConcurrentModificationException 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.ConcurrentModificationException
      at java.util.AbstractList$Itr.checkForComodification(Unknown Source)
      at java.util.AbstractList$Itr.next(Unknown Source)
      at ie.gmit.game.GameModel.checkEndGame(GameModel.java:155)
      at ie.gmit.game.GameModel$TimeCounterListener.actionPerformed(GameModel.java:301)
      at javax.swing.Timer.fireActionPerformed(Unknown Source)
      at javax.swing.Timer$DoPostEvent.run(Unknown Source)
      at java.awt.event.InvocationEvent.dispatch(Unknown Source)
      at java.awt.EventQueue.dispatchEvent(Unknown Source)
      at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
      at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
      at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
      at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
      at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
      at java.awt.EventDispatchThread.run(Unknown Source)

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);
		}
	}
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
> ound 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()

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.
Avatar of satory

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);  
Iterator i = M_men.iterator();
		while (i.hasNext())
		{
			Merchantmen m = (Merchantmen)i.next();
			if (m.isAlive()==false)
			{				
				M_men.remove(i);
			}
		}

Open in new window

>                         M_men.remove(i);

should be:

i.remove();
Avatar of satory

ASKER

cheers...

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
:-)