Getting java.util.ConcurrentModificationException

R

Roiysh

I have written a game that using min max algorithm, with Vector as
it's tokens and in the algorithm it makes a move and check it's value
and then return to original values, but when i try to use the vector
again it gives me the exception above, that i altered it and therefor
i cannot continue.
i have tried to create a clone of the vetctor so it won't be using the
original vactor and work on the copy but it didnt work.
someone have an idea to help me outa here i am helpless.
thanks,
Roi .
 
R

RedGrittyBrick

I have written a game that using min max algorithm, with Vector as
it's tokens and in the algorithm it makes a move and check it's value
and then return to original values, but when i try to use the vector
again it gives me the exception above, that i altered it and therefor
i cannot continue.
i have tried to create a clone of the vetctor so it won't be using the
original vactor and work on the copy but it didnt work.
someone have an idea to help me outa here i am helpless.
thanks,

Having multiple threads can make for problems. But often good use of
threads is essential.

I'd start here
http://java.sun.com/docs/books/tutorial/essential/concurrency/

Others in this newsgroup have recommended the book "Java Concurrency in
Practice" by Brian Goetz. I haven't read it.

Joshua Bloch's "Effective Java" (2nd Ed) covers concurrency in Chapter 10.

Maybe you are modifying your Vector in one thread whilst an iterator is
being used in another? Maybe your clone is shallow?

For more direct help, I'd create an SSCCE - http://sscce.org


P.S. Whilst your English is far far better than my <insert other
language of your choice here>, long sentences with poor capitalisation
and punctuation are harder for me to read. Please take your time when
composing a question.
See http://www.catb.org/~esr/faqs/smart-questions.html
 
J

Joshua Cranmer

I have written a game that using min max algorithm, with Vector as
it's tokens and in the algorithm it makes a move and check it's value
and then return to original values, but when i try to use the vector
again it gives me the exception above, that i altered it and therefor
i cannot continue.

A few points to make. First, avoid using Vector; LinkedList or ArrayList
is probably better-suited to your purpose. The former is deprecated in
all but name; the latter were added in the introduction of Collections
and fit much more nicely in that paradigm.

Second, could you post an SSCCE (short, simple, complete code example)
of what's going on? It's hard to infer what is causing the problem with
vague statements.

ConcurrentModificationExceptions arise when you are iterating a
collection, and also mutating [1] it outside of the iterator, which then
confuses the iterator. The general solution, then, is to either use the
iterator to do all the mutations (only practical if you're mutating
surrounding elements), or to roll your own iteration loop which keeps
track of the mutations.

[1] I am limiting the definition of mutation here to refer only to the
list itself, and not objects within the list. So adding, inserting, or
removing will cause this exception, and possibly setting as well.
 
G

GArlington

I have written a game that using min max algorithm, with Vector as
it's tokens and in the algorithm it makes a move and check it's value
and then return to original values, but when i try to use the vector
again it gives me the exception above, that i altered it and therefor
i cannot continue.
i have tried to create a clone of the vetctor so it won't be using the
original vactor and work on the copy but it didnt work.
How "it didn't work"?
Nothing changed => your copy was NOT a complete clone, see "your clone
is shallow" (by RGB).
Make sure that you clone ALL data (deep clone), then it will work...
someone have an idea to help me outa here i am helpless.
There are few explanations why it does not work the way you programmed
it higher in this thread...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top