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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
Can't be done with for each. Use an Iterator (i) and call i.remove on it
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mick BarryJava DeveloperCommented:
> 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
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Mick BarryJava DeveloperCommented:
>                         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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.