the java concurrent modification exception debacle of 2010

老子叫甜甜 提交于 2019-12-23 12:44:00

问题


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


回答1:


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.




回答2:


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




回答3:


You get an error because you are removing particles from the list inside the for-each loop for the same list. The indexes and length are being changed from within the loop, and Java doesn't know how to handle that. Either loop through the particles a different way, or don't remove them like that



来源:https://stackoverflow.com/questions/4097217/the-java-concurrent-modification-exception-debacle-of-2010

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!