Right, so at some point I found this piece of code, in here, to maintain an active list of elements (I currently use this code):
for(Entity e : this.entities) {
if(e.isActive()) {
e.update();
if(e.isActive()) {
this.stillActive.add(e);
}
}
}
ArrayList<Entity> tmp = this.entities;
this.entities = this.stillActive;
this.stillActive = tmp;
this.stillActive.clear();
The stuff that happens in the for loop is pretty straight forward, however I’m not sure I get the last bit, with swapping pointers and what not(At least, that’s what I assume it does, as to switch the elements from stillActive over into entities).
Today, however, I ran into the retainAll() function, that’s defined in the Set interface, which does exactly the same.
So instead of the above code, I could write:
for(Entity e : this.entities) {
if(e.isActive()) {
e.update();
if(e.isActive()) {
this.stillActive.add(e);
}
}
}
this.entities.retainAll(this.stillActive);
this.stillActive.clear();
which imo is a lot more readable and understandable. My question is, which method is better?
Cheers!