Using the following code will result in a java.util.ConcurrentModificationException
final AnimationTimer timer = new AnimationTimer() {
@Override
public void handle(long timestamp) {
for (ListIterator<myCircle> List = CircleList.listIterator(); List.hasNext(); ) {
myCirle check = List.next();
if (CheckCollisionFunction(check) == true) {
this.stop();
CircleList.clear();
gameOverFunction();
}
}
}
}
So im using an iterator to check if any of my circles are colliding with my player circle.
When I clear the list:
CircleList.clear();
I get the exception.
java.util.ConcurrentModificationException
Which is when you delete sometimes outside an iterator when using an iterator. Can someone please show me how to delete everything in my list using the iterator?
Or another way to stop this error.
This is my list.
private final ObservableList<myCircle> CircleList = FXCollections.observableArrayList();