Help with a short game code

when i try to remove a part of a arraylist it says the error “ConcurrentModificationException”

here is the file : https://drive.google.com/open?id=1-NATnwPv0Qw3abEjZzP8rBFMXgwngVrL

here is what gives the error

for(Laser actualLaser: laserList){
  println(actualLaser);
	for(int i = 0; i<4; i++){
		if(actualLaser.x > escudoArray[i].x-50 &&
			 actualLaser.x < escudoArray[i].x+50 &&
			 actualLaser.y > escudoArray[i].y-5 &&
			 actualLaser.y < escudoArray[i].y+5 &&
			 actualLaser.vel > 0){
			escudoArray[i].y = -500;
			laserList.remove(actualLaser);
		}
	}
1 Like

We can’t use method remove() when using an “enhanced” for ( : ) {} style! :no_entry_sign:

Use a vanilla for ( ; ; ) {} style instead. Preferably, w/ backwards iteration: :back:

4 Likes

couldn’t you use an iterator as well? something like

Iterator<String> it = myList.iterator();
while (it.hasNext()) { 
  if ("SOMESTRING".equals(it.next())) { 
    it.remove();
  }
}

might be slower on larger lists and i gotta say a for loop running n-0 is my preferred way too but the more options the better…right :confused:

1 Like

The reason I prefer not to rely on an Iterator is b/c it’s incompatible w/ Pjs, in case I decide to deploy my sketch online as well. :spider_web:

2 Likes

well that makes sense.