majR
1
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!
Use a vanilla for ( ; ; ) {}
style instead. Preferably, w/ backwards iteration:
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
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.
2 Likes