IndexOutOfBounds index: 7 size: 7 [SOLVED]

Which one works?

This one works:

ArrayList<Bullet> nulledBullets = new ArrayList<Bullet>();
   ArrayList<Enemy> nulledEnemys = new ArrayList<Enemy>();
   
   public void update(ArrayList<Enemy> e, EnemyController ec, Game game) {
      if (nulledEnemys.size() > 0) {
         for (Enemy nulledEnemy : nulledEnemys) {
            if (e.contains(nulledEnemy)) {
               e.remove(nulledEnemy);
            }
         }
         nulledEnemys.clear();
      }
      if (nulledBullets.size() > 0) {
         for (Bullet nulledBullet : nulledBullets) {
            if (b.contains(nulledBullet)) {
               b.remove(nulledBullet);
            }
         }
         nulledBullets.clear();
      }
      for (Bullet bullet : b) {
         if (b != null) {
            for (Enemy enemy : e) {
               if (enemy != null) {
                  if (bullet.getBounds().intersects(enemy.getBounds())) {
                     if (!nulledBullets.contains(bullet)) {
                        nulledBullets.add(bullet); // THIS WILL REMOVE BULLETS.
                     }
                     // removeBullet(bullet); <--- REMOVED
                     game.enemyHealth--;
                     if (game.enemyHealth <= 0) {
                        if (!nulledEnemys.contains(enemy)) {
                           nulledEnemys.add(enemy); // THIS WILL REMOVE ENEMYS.
                        }
                        // ec.removeEnemy(enemy); <--- REMOVED
                        game.score++;
                     }
                  }
               }
            }
            bullet.update(e);
         }
      }
   }

no your one worked. Thanks

Well they both sort of use the same idea. “ConcurrentModificationException” is getting thrown since you are removing stuff from the list at the same time as you are iterating over it.

What we do here is we create a temporary list to hold the object that are not removed (my method) or in Gabriel’s method he stores the faulty enemies in an array and checks them before looping through the thingy.

In any case, my code still doesn’t deal with the enemies being removed.

If it works – great!

gasp ;D

Well, this was something that I came up with a while ago when dealing with Bullets and Collision myself >:D.

What’s going on is since we switched to:


// This:
for (Bullet b : bullets) {

}

// Instead of This:

for (int i = 0; i < bullets.size(); i++) {

}

It will throw a Exception (Due to the fact that we’re currently looping through the data in the ArrayList, so we can’t remove it).
So, I needed a way to remove those bullets from the list after the collision without getting the Exception.
We created a List of NulledBullets, everytime we want to remove a bullet we add it to that list.
And the next time the method restarts it will check if the NulledBullets contains any bullets, if it does it will treat them as un-wanted and remove them from the list.

(The fix was removing them at the beginning of the method vs. removing them while we’re looping through them).

Once again, just woke up so I hope everything’s readable.
Glad I could help mate :smiley:

EDIT
: Where the hell… did that horned smiley face come from???
It’s not even on the smiley face list LOL

Haha, Thanks for explaining it. really helped :slight_smile:

Another popular alternative is to maintain two lists. Copy the items you want to keep into the new list as you go through it and leave out the ones you don’t, then alternate lists every time. Basically a home-grown semispace collector. It does take double the amount of space for references, but those are usually pretty small.

I give up

Yeah something like this

   public void update(ArrayList<Enemy> e,  EnemyController ec, Game game){
      
      List<Enemy> tempEnemy = new ArrayList<Enemy>( e.size() );
      List<Bullet> tempBullet = new ArrayList<Bullet>( b.size() );
      
      for (  Bullet bullet : b ) {

            for (Enemy enemy : e) {

               if ( bullet.getBounds().intersects( enemy.getBounds() ) ) {

                  game.enemyHealth--;

                  if ( game.enemyHealth <= 0 ) {

                     //ec.removeEnemy( enemy );
                     game.score ++;

                  } else {
                     // don't remove enemy
                     tempEnemy.add( enemy );
                  }
               } else {
                   // don't remove bullet.
                   tempBullet.add( bullet );
               }
            }
         
         e.clear();
         e = tempEnemy;
         tempEnemy.clear();

         bullet.update(e);
      }

      b.clear();
      b = tempBullet;
      tempBullet.clear();
   }