[Q] ConcurrentModificationException

  • Thread starter Michael T. Peterson
  • Start date
M

Michael T. Peterson

Hi all,

While running a non-threaded program (at least my program doesn't create any
threads), I sustained a ConcurrentModificationException. More specifically,
the code is a simple while-loop using an iterator on a vector - Here is the
code.

while( it.hasNext() == true )
{
NodeToken nt = (NodeToken) it.next();
CIMFlavor cimFlavor = this.getFlavor( nt.tokenImage );
flavors.add( cimFlavor );
}

If I implement this same logic using a for loop, the loop overruns its index
and throws a ClassCastException. NB: The vector contains 2 elements and the
value of the index when the ClassCastException is taken is 2 (i.e., looking
for a 3rd element).

Here is the version of Java I'm using:

java version "1.4.1_02"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1_02-b06)
Java HotSpot(TM) Client VM (build 1.4.1_02-b06, mixed mode)

Can someone help me understand what's going on here. Have I missed something
really basic?

Cheers,

Michael
 
V

VisionSet

Michael T. Peterson said:
Hi all,

While running a non-threaded program (at least my program doesn't create any
threads), I sustained a ConcurrentModificationException. More specifically,
the code is a simple while-loop using an iterator on a vector - Here is the
code.

while( it.hasNext() == true )
{
NodeToken nt = (NodeToken) it.next();
CIMFlavor cimFlavor = this.getFlavor( nt.tokenImage );
flavors.add( cimFlavor );
}

From API
"The Iterators returned by Vector's iterator and listIterator methods are
fail-fast: if the Vector is structurally modified at any time after the
Iterator is created, in any way except through the Iterator's own remove or
add methods, the Iterator will throw a ConcurrentModificationException."
Does that make sense of your problem? Your code sample isn't complete
enough to judge.
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top