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

satoryAsked:
Who is Participating?
 
CEHJConnect With a Mentor Commented:
Can't be done with for each. Use an Iterator (i) and call i.remove on it
0
 
objectsCommented:
> 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.
0
 
satoryAuthor Commented:
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

0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
objectsCommented:
>                         M_men.remove(i);

should be:

i.remove();
0
 
satoryAuthor Commented:
cheers...

i should of gone back to look at the first response after i found out how to use an iterator
0
 
CEHJCommented:
>>M_men.remove(i);

>>Use an Iterator (i) and call i.remove on it
0
 
CEHJCommented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.