Re: Getting java.util.ConcurrentModificationException
Roiysh@gmail.com wrote:
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.
--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth