I am trying to Iterate two ArrayList: One of them, called "gegner" is a list about enemys and another one called "waende" is about walls.
Whenever a wall and an Enemy touch, both should loose one durability / health. As it didn't work at the same time, I created a new method that should remove the death Objects from the Lists.
My idea was:
public void removeDeathObjects() {
Wand tempW;
Gegner tempG;
for (Iterator<Gegner> it = gegner.iterator(); it.hasNext();) {
tempG = it.next();
for (Iterator<Wand> it2 = waende.iterator(); it2.hasNext();) {
tempW = it2.next();
if(tempW.isDestroyed()){
it2.remove();
}
if (tempG.isDeath()){
it.remove();
}
}
}
}
But the program throws an "Exception in thread "AWT-EventQueue-0" in the Line it.remove() java.lang.IllegalStateException" as soon as there are at least two walls, and the enimies aren't killed completely.
Where did I failed?
If you wanted, I could give you the whole code but it's pretty long ._.
PS: sorry for bad English
gegner.removeIf(g -> g.isDeath())?