nature-of-code / noc-examples-processing

Repository for example code from The Nature of Code book
MIT License
2.56k stars 951 forks source link

Iterator #13

Open shiffman opened 11 years ago

shiffman commented 11 years ago

Need to decide whether to use Iterator in the examples or not. It needs an explicit import now. It seems to be supported by Processing.js but maybe less risk to iterate with i.

Iterator<Particle> it = particles.iterator();
while (it.hasNext()) {
  Particle p = it.next();
  p.run();
  if (p.isDead()) {
     it.remove(); 
   }
}

vs.

for (int i = particles.size()-1; i >=0; i--) {
  Particle p = particles.get(i);
  p.run();
  if (p.isDead()) {
    particles.remove(i);
  }
}
tecnecollective commented 11 years ago

Iterators work faster, but I think that is good to understand the implications of an iteration to use an decreasing for loop