2010 Java Concurrent Exception Exception Error

Painting some particles stored in an ArrayList. This code works fine:

super.paintComponent(g); for (Particle b: particleArr){ g.setColor(b.getColor()); g.fillOval(b.getXCoor() + 5,b.getYCoor(), b.getParticleSize(),b.getParticleSize()); } 
However this code throws a concurrent modification exception:
 public void paintComponent(Graphics g){ //paint particles super.paintComponent(g); for (Particle b: particleArr){ g.setColor(b.getColor()); if (b.isDead()) particleArr.remove(b); else if (!b.isVanishing()) g.fillOval(b.getXCoor(),b.getYCoor(), b.getParticleSize(),b.getParticleSize()); else { g.fillOval(b.getXCoor() + 5,b.getYCoor(), b.getParticleSize(),b.getParticleSize()); g.fillOval(b.getXCoor() - 5,b.getYCoor(), b.getParticleSize(),b.getParticleSize()); g.fillOval(b.getXCoor(),b.getYCoor() + 5, b.getParticleSize(),b.getParticleSize()); g.fillOval(b.getXCoor(),b.getYCoor() - 5, b.getParticleSize(),b.getParticleSize()); } } 
I confused. This is the garbled code with the iterator, it is running slow.
 itr = particleArr.iterator(); 
  super.paintComponent(g); while (itr.hasNext()){ particle=itr.next(); g.setColor(particle.getColor()); if (particle.isDead()) itr.remove(); else if (particle.isVanishing()) g.fillOval(particle.getXCoor(),particle.getYCoor(), particle.getParticleSize(),particle.getParticleSize()); else { g.fillOval(particle.getXCoor() + 5,particle.getYCoor(), particle.getParticleSize(),particle.getParticleSize()); g.fillOval(particle.getXCoor() - 5,particle.getYCoor(), particle.getParticleSize(),particle.getParticleSize()); g.fillOval(particle.getXCoor(),particle.getYCoor() + 5, particle.getParticleSize(),particle.getParticleSize()); g.fillOval(particle.getXCoor(),particle.getYCoor() - 5, particle.getParticleSize(),particle.getParticleSize()); } 

+4
source share
3 answers

Try to get an Iterator from the list of arrays, and then call the remove () method on the iterator to remove the element.

Example

 Iterator itr = particleArr.iterator(); while(itr.hasNext()) { Particle b = (Particle)itr.next(); if (b.isDead()) itr.remove(); } 

Edit: just made the example more appropriate for your code.

+14
source

You cannot iterate over each item in the collection and delete the item:

  if (b.isDead()) particleArr.remove(b); 

You can copy Colletion to another, first:

 ArrayList copy = new ArrayList(particleArr); for (Particle b: copy){ 

Or you can try wrapping your collection with CopyOnWriteArrayList http://download.oracle.com/javase/6/docs/api/java/util/concurrent/CopyOnWriteArrayList.html

Or you can use an iterator (this might be the best way):

 Iterator itr = particleArr.iterator(); while(itr.hasNext()) { Particle b = (Particle)itr.next(); if (b.isDead()) itr.remove(); } 

edit: Took an iterator in options

+4
source

You get an error because you are removing particles from the list inside the for-each loop for the same list. Indexes and lengths change out of cycle, and Java does not know how to handle this. Either loop the particles differently, or don’t remove them this way.

+2
source

All Articles