From 95abed351f387cb8684158812e010cbacf2e647e Mon Sep 17 00:00:00 2001 From: Daniel Shiffman Date: Wed, 6 Mar 2013 22:38:01 -0500 Subject: [PATCH] remove iterator from particle system --- .../MultipleParticleSystems.pde | 1 - .../MultipleParticleSystems/ParticleSystem.pde | 16 ++++++++-------- 2 files changed, 8 insertions(+), 9 deletions(-) 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