diff --git a/java/examples/Topics/Simulate/MultipleParticleSystems/MultipleParticleSystems.pde b/java/examples/Topics/Simulate/MultipleParticleSystems/MultipleParticleSystems.pde index 5acfc96e6..5bc038e7d 100644 --- a/java/examples/Topics/Simulate/MultipleParticleSystems/MultipleParticleSystems.pde +++ b/java/examples/Topics/Simulate/MultipleParticleSystems/MultipleParticleSystems.pde @@ -15,7 +15,6 @@ ArrayList systems; void setup() { size(640, 360); systems = new ArrayList(); - smooth(); } void draw() { diff --git a/java/examples/Topics/Simulate/MultipleParticleSystems/ParticleSystem.pde b/java/examples/Topics/Simulate/MultipleParticleSystems/ParticleSystem.pde index d387e8444..570551009 100644 --- a/java/examples/Topics/Simulate/MultipleParticleSystems/ParticleSystem.pde +++ b/java/examples/Topics/Simulate/MultipleParticleSystems/ParticleSystem.pde @@ -1,6 +1,5 @@ // An ArrayList is used to manage the list of Particles // An Iterator is used to remove "dead" particles while iterating over the list -import java.util.Iterator; class ParticleSystem { @@ -15,14 +14,14 @@ class ParticleSystem { } } + void run() { - // Cycle through the ArrayList using Iterator, because we are deleting while iterating - Iterator it = particles.iterator(); - while (it.hasNext()) { - Particle p = it.next(); + // Cycle through the ArrayList backwards, because we are deleting while iterating + for (int i = particles.size()-1; i >= 0; i--) { + Particle p = particles.get(i); p.run(); if (p.isDead()) { - it.remove(); + particles.remove(i); } } } @@ -32,7 +31,8 @@ class ParticleSystem { // Add either a Particle or CrazyParticle to the system if (int(random(0, 2)) == 0) { p = new Particle(origin); - } else { + } + else { p = new CrazyParticle(origin); } particles.add(p); @@ -46,5 +46,5 @@ class ParticleSystem { boolean dead() { return particles.isEmpty(); } +} -} \ No newline at end of file