Here’s my code:
public ArrayList<PlayerCharacter> goodGuyEntities = new ArrayList<PlayerCharacter>();
public ArrayList<BadGuy> badGuyEntities = new ArrayList<BadGuy>();
public ArrayList<Shot> goodGuyShotEntities = new ArrayList<Shot>();
public ArrayList<Shot> badGuyShotEntities = new ArrayList<Shot>();
public ArrayList<Shot> removeList = new ArrayList<Shot>();
for (PlayerCharacter character : goodGuyEntities) {
character.update();
}
for (BadGuy badGuy : badGuyEntities) {
badGuy.update();
}
for (Shot shot : goodGuyShotEntities) { // this is the line that eclipse said threw the exception
shot.update();
for (BadGuy badGuy : badGuyEntities) {
if (shot.bounds.intersects(badGuy.bounds)) {
shot.hit(badGuy);
}
}
}
for (Shot shot : badGuyShotEntities) {
shot.update();
for (PlayerCharacter character : goodGuyEntities) {
if (shot.bounds.intersects(character.bounds)) {
shot.hit(character);
}
}
}
for (Entity remove : removeList) {
removeEntity(remove);
}
This threw a ConcurrantModificationException and as far as I understand the exception, it’s because I’m iterating through the ArrayLists and calling update() on each object in the ArrayList within the iteration. Is there any way to get around this besides using the traditional for loop:
for(int i = 0; i < goodGuyShotEntities.size(); i++)
this fixes it but I don’t like how it looks and is much harder to use and read.