Java IndexOutOfBoundsException

java

Solution

I highly recommend against playing with the for loop counters from within the loop itself. You are careful now, you won't be careful later ("let's try a hack here to debug") and end up with bugs.

One solution could be:

- check whether two objects intersect

- if they do, save a reference into a separate list of `thingsToRemove`

- finally, go through the `thingsToRemove` and remove (or set to 'null', or `-1` or whatever) the corresponding elements in the `cb` and `b` lists

Problem

I made an little shoot em up game..It works normal but I want also implement if fires intersects they will disappear. I have two list for Player bullets and for computer bullets ...But if I have more bullets from computer or reverse .Here my loop ``` for (int i = 0; i < cb.size(); i++) { for (int j = 0; j < b.size(); j++) { if (b.get(j).rect.intersects(cb.get(i).rect)) { cb.remove(i); b.remove(j); continue; } if (cb.get(i).rect.intersects(b.get(j).rect)) { b.remove(j); cb.remove(i); continue; } } } ``` This is my game which woking algoritms... http://rapidshare.com/files/364597095/ShooterGame.2.6.0.jar

Original source