EDIT: Ohh dude, you're calling remove twice probably. Duh.
for(Iterator<Bullet> it = bullet.iterator(); it.hasNext(); bullet = it.next()) {
if(bullet.rectangleBullet.x > screenSize || bullet.rectangleBullet.x < 0) {
it.remove();
}
}
[background=#fafbfc]Doing two ifs separately there makes it so you'll be calling 'remove' twice for bullets that meet both conditions. That throws an error obviously since you can't remove the same element twice. So you should remove once if it meets any of the two conditions.[/background]
Indeed, but you'd hope that screenSize is positive, which implies you never remove it twice in the original code. Your solution of course makes sure it works even for non-positive screen sizes :)
My guess is that the problem is elsewhere, it's just detected here. @OP: Maybe you call a function in this loop that also loops over the list? (EDIT: Or the other way around, a function that iterates over the list calls this function? Not sure that could cause this exception)
The above solution is much nicer, so use that rather than mine, but if you don't care about the order in the bullets list, and you don't want to create temporary lists, another solution is to move the last entry to the deleted spot, like
int last = bullets.size() - 1; // Do yourself a favor, make lists a multiple rather than a singular word.
int i = 0;
while (i <= last) {
Bullet bullet = bullets.get(i); // So you can have singular bullets use the singular word.
if(bullet.rectangleBullet.x > screenSize || bullet.rectangleBullet.x < 0) {
if (i < last) { bullets.set(i, bullets.get(last)); }
last = last - 1;
// Do not increment i so the next iteration inspects the bullet we just moved here.
} else {
i = i + 1;
}
}
// Minor problem, bullets[last] is now the last bullet, but bullets.size() may be larger, and Java has no truncate-list operation.
// Instead, remove the last element repeatedly
int new_size = last + 1;
while (bullets.size() > new_size) { bullets.remove(bullets.size() - 1); }
Untested, so it may contain some typos.
As you can see, it's much longer and somewhat convoluted, but it's a possibility.