Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

the java concurrent modification exception debacle of 2010

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());
              }

like image 358
user455497 Avatar asked Nov 30 '22 09:11

user455497


2 Answers

Try getting an Iterator from the array list then calling the remove() method on the iterator to remove the item.

Example

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

Edit: Just made the example a bit more relevant to your code.

like image 169
3urdoch Avatar answered Feb 15 '23 00:02

3urdoch


You can't iterate with an for-each over an Collection AND remove an Element:

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

You may copy your Colletion into another one, first:

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

Or you can try to Wrap your Collection with an CopyOnWriteArrayList http://download.oracle.com/javase/6/docs/api/java/util/concurrent/CopyOnWriteArrayList.html

Or you can use an iterator (This may be the best way):

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

edit: Took the iterator to the options

like image 39
Christian Kuetbach Avatar answered Feb 15 '23 01:02

Christian Kuetbach