I'm trying to remove an object from an iterating ArrayList but cannot do so from inside of the loop, here is what I have at the moment
for(Pearl pearl : this.pearls){
pearl.onDraw(canvas);
if(fish.isCollide(pearl)){
this.pearls.remove(pearl);
}
}
The above code does not work if the ArrayList is larger than 1.
I was thinking of changing the code to something such as the following but would prefer to know if there is a simpler way.
List<Pearl> pearls_delete = new ArrayList<Pearl>();
for(Pearl pearl : this.pearls){
pearl.onDraw(canvas);
if(fish.isCollide(pearl)){
pearls_delete.add(pearl);
}
}
this.pearls.removeAll(pearls_delete);