FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

remove iterator from particle system · biddy/processing@304e51f · GitHub

Repository navigation

Commit 304e51f

Browse files
committed
remove iterator from particle system
1 parent 3452def commit 304e51f

2 files changed

Lines changed: 8 additions & 9 deletions

File tree

‎java/examples/Topics/Simulate/MultipleParticleSystems/MultipleParticleSystems.pde‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ ArrayList<ParticleSystem> systems;
1515
void setup() {
1616
size(640, 360);
1717
systems = new ArrayList<ParticleSystem>();
18-
smooth();
1918
}
2019

2120
void draw() {

‎java/examples/Topics/Simulate/MultipleParticleSystems/ParticleSystem.pde‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// An ArrayList is used to manage the list of Particles
22
// An Iterator is used to remove "dead" particles while iterating over the list
3-
import java.util.Iterator;
43

54
class ParticleSystem {
65

@@ -15,14 +14,14 @@ class ParticleSystem {
1514
}
1615
}
1716

17+
1818
void run() {
19-
// Cycle through the ArrayList using Iterator, because we are deleting while iterating
20-
Iterator<Particle> it = particles.iterator();
21-
while (it.hasNext()) {
22-
Particle p = it.next();
19+
// Cycle through the ArrayList backwards, because we are deleting while iterating
20+
for (int i = particles.size()-1; i >= 0; i--) {
21+
Particle p = particles.get(i);
2322
p.run();
2423
if (p.isDead()) {
25-
it.remove();
24+
particles.remove(i);
2625
}
2726
}
2827
}
@@ -32,7 +31,8 @@ class ParticleSystem {
3231
// Add either a Particle or CrazyParticle to the system
3332
if (int(random(0, 2)) == 0) {
3433
p = new Particle(origin);
35-
} else {
34+
}
35+
else {
3636
p = new CrazyParticle(origin);
3737
}
3838
particles.add(p);
@@ -46,5 +46,5 @@ class ParticleSystem {
4646
boolean dead() {
4747
return particles.isEmpty();
4848
}
49+
}
4950

50-
}

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL