Array index out of bounds

I don't know what I'm missing... the full code is pretty big so I'll just put the part throwing exceptions here.

      for (int i = 0; i < player.getBullets().size(); i++) { //for every player bullet
            for (int j = 0; j < aliens.getAliens().size(); j++) { //for every player bullet every alien
                player.getBullets().get(i);
                aliens.getAliens().get(j);
                if (player.getBullets().get(i).getBounds().intersects(aliens.getAliens().get(j).getBounds())){ //player bullet vs alien collison
                    if (aliens.getAliens().get(j).getType() == 1)
                        score += 2 + level;
                    else if(aliens.getAliens().get(j).getType() == 2)
                        score += 4 + level;
                    else
                        score += 8 + level*2;
                    aliens.getAliens().remove(j); // alien dies
                    player.getBullets().remove(i);
                    System.out.println("player bullet removed");
                }
            }
        }       

It threw an exception on the //player bullet vs alien collision line. I added in player.getBullets().get(i) and the getAliens() to figure which one threw the exception and it was the player one.

Error seems kind of random, but I believe it goes out of bounds when an alien moves sideways onto a bullet. It works fine if the bullet hits the alien straight on. Could this be an error with using intersects?

By the way, this is a Space Invaders mimic. Any help would be appreciated.

Answers


You're altering your lists as you're traversing them (by calling .remove() inside the loop). You're deleting the bullet in the middle of checking for collisions with it.

One solution is to give aliens/bullets/other such objects an isDead variable. Then, instead of deleting them inside the loop, wait until after the loop and delete everyone with isDead == true.


Use Iterator which supports remove operation on it's collection. To give you an idea (all irrelevant code from OP removed):

    List<Bullet> bullets = new ArrayList<Bullet>();
    List<Alien> aliens = new ArrayList<Alien>();

    Iterator<Alien> alienIterator = aliens.iterator();
    Iterator<Bullet> bulletIterator = bullets.iterator();
    while (bulletIterator.hasNext()) { //for every player bullet
        while (alienIterator.hasNext()) { //for every player bullet every alien
            Bullet bullet = bulletIterator.next();
            Alien alien = alienIterator.next();
            if (bullet.getBounds().intersects(alien.getBounds())) { //player bullet vs alien collison
                bulletIterator.remove();
                alienIterator.remove();
                System.out.println("player bullet removed");
            }
        }
    }

Note that it's still susceptible to IllegalStateException if you modify aliens/bullets somewhere else concurrently.


Need Your Help

Spring Mqtt - Publish messages to multiple topics programmatically

spring spring-integration mqtt paho

How can I publish messages with different topics programmatically?

Reading Math Functions in Assembly

c math assembly x86 instructions

I'm translating a bit of x86 Assembly to C code. A small section of the assembly is giving me trouble.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.