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());
}
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With