Remove element while being used

I have a player which can shoot bullets, and I have made it so that when the bullet is out of the frame the bullet gets removed. But when I run the program I get the ConcurrentModificationException error. I have all of my bullets stored in an ArrayList. Is there a simple way to remove the bullets?

Also my technique for game design is still quite basic. Could anyone link me to easy to understand tutorials(preferably video) for 2d game programming?

Thanks for the help

You get a ConcurrentModificationException because you cannot remove an element while using the for-each loop.

To bypass that, use the remove() method that is built in the Iterator:


Iterator<MyType> i = myArrayList.iterator();
while(i.hasNext()) {
    MyType object = i.next();
    
    if(objectHasToBeRemoved)
        i.remove();
}

EDIT: Oh by the way if you didn’t know, the for-each loop is only compiler magic, the bytecode is actually transformed to use that Iterator class :smiley:

Easier yet, just change the ArrayList to a CopyOnWriteArrayList.

-Nathan

So you’re trading speed and efficiency for ease of use? CopyOnWriteArrayList creates a new internal array for all the mutative methods!!

Well for the stuff I’m working with, speed and efficiency aren’t exactly high on my priority list, as it can run upwards of 6k fps uncapped.

-Nathan

just screw those iterators

I never knew primitive ints had remove methods!

@Cero
You need to subtract 1 from “i” after removing it from the list :wink:

If you’re doing it index-based just count backwards to 0.

But you almost always want to like set a boolean as in the first example and then after doing updates remove anything that is marked for removal. This is because theoretically every entity acts simultaneously, so you don’t want someone to get deleted before someone else can be effected by him, just because the deleted guy happens to be at a lower index. Make sense?

But I feel like I got this error for something not related to the loop. Let me try to remember what it was. Nope can’t remember. That’s from like 5 years ago. :stuck_out_tongue:


for(int i = 0; i < myArrayList.size(); ) {
// do stuff
if(obectHasToBeRemoved) {
myArrayList.remove(i);
} else {
i++;
}
}

Works better than ‘subtracting’.

However, what if you remove in multiple places in the loop? Your method will become too clumsy. Adding an “i–” after every remove is the only sensible solution :stuck_out_tongue:

Eck, if you’re removing multiple things per iteration there’s probably something wrong with your system.

Or just iterate backwards:


for (int i = myArrayList.size() - 1; i >= 0; i--) {
   // do stuff

   if (needsRemoving) {
      myArrayList.remove(i);
   }
}

Everything else is clumsier and more error prone, except for the flag for removal and remove later scenario, which is valid if you need to process everything one stage at a time.

Um… what? What if your making an MMO and you have a area based attack that takes out multiple enemies at once? Or, what if your playing a simple space shooter and you fire three lasers at once, for a power-up or something, and the three lasers hit three entities at once?

Well, I’d think that the iteration through the list would take care of it, as opposed to having an iteration inside of the iteration.

I mean, I can probably think up a reason for doing it, but it feels like if you’re doing it, then there’s probably a better way to do it than that.

OMG. What I said and lhkbob seconded - that’s how you do it.

By not mentioning performance, add the will be removed object to 2nd arraylist and use first arraylist’s removeAll().

or list.clear();

Hell it’s an ArrayList. Those are fast as hell, at least for my 2D game. I do this all the time I don’t even get ONE millisecond for one frame (And this with no optimizations like don’t draw objects out of view [only the map is rendered a bit optimized]).

well 1ms would be A LOT.
60 fps games have only 16.6ms for everything, naturally you want to use way less, so you can breathe - and the pc your developing on is certainly not the slowest you want to support

well yeah, but it really depends on what you are doing alltogether
I use pure arrays
I have used ArrayLists too, with the remove later in a seperate loop approach

I vote for CopyOnWriteArrayList.

Thanks for the help. I am using the Iterator method currently, but please keep posting because I find this very informative.

Also could anyone tell my how I can figure out what FPS my game is running at?

Thanks