As @glv’s already pointed out, you need to import the Iterator interface before using it:
import java.util.Iterator;
Alternatively you can use a regular for ( ; ; )
loop, but making sure to decrease its iterator variable each time you invoke method remove():
for (int i = 0; i < particles.size(); ++i) {
final Particle p = particles.get(i);
p.run();
if (p.isDead()) particles.remove(i--);
}
It’s even simpler if you iterate it backwards, so you don’t have to worry about decreasing the iterator for each remove():
for (int i = particles.size(); i-- > 0; ) {
final Particle p = particles.get(i);
p.run();
if (p.isDead()) particles.remove(i);
}
For a more complex but more efficient (b/c we completely avoid container’s element left-shifting) backwards technique, see this sketch’s mousePressed() code block implementation:
- “Clickable Balls”: studio.ProcessingTogether.com/sp/pad/export/ro.PKnDDqu1vYo