# Array out of range exeption into School project

**URL:** <https://discourse.processing.org/t/array-out-of-range-exeption-into-school-project/20133>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [April 25, 2020, 10:47am UTC](https://discourse.processing.org/t/array-out-of-range-exeption-into-school-project/20133 "2020-04-25T10:47:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jestsect](https://avatars.discourse-cdn.com/v4/letter/j/e47774/32.png) [@Jestsect](https://discourse.processing.org/u/Jestsect)\
**Post date:** [April 25, 2020, 10:47am UTC](https://discourse.processing.org/t/array-out-of-range-exeption-into-school-project/20133/1 "2020-04-25T10:47:44Z")

</div>

Hello,  
I’m trying to make a Space invader like game for a school project, but I have a problem with collisions: I have an arry wich contain shot’s coordonates and an other which contain ennemis oordonates. Ennemies spawns regulary and shots spawn faster than them. if you just clic on each ennemi It work, but if you play with auto fire and miss some aliens, the game crash. I don’t know how to resolve the “array out of bound exetion” error. It seem like when autofire is activate, one array contain more cases than the other, but after weeks of reseach I don’t even know how to make it function.  
I use two nested/embedded ( I’m not english, I don’t know the right word) “for” which run each aliens for each shots.

Thank you in advance for helping.

---

<div class="post-metadata">

**Author:** ![SomeOne](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/someone/32/8639_2.png) [@SomeOne](https://discourse.processing.org/u/SomeOne)\
**Post date:** [April 25, 2020, 12:05pm UTC](https://discourse.processing.org/t/array-out-of-range-exeption-into-school-project/20133/2 "2020-04-25T12:05:24Z")

</div>

Hi Jestsect,

Welcome to forum.

It sound you have identified problem. Array out of range exception means that program has tried to use array with an index value bigger than array size. It’s a bit difficult to tell anything else without the code.

Usually when you go through arrays with for loops structure is like this

```auto
for( int i = 0; i < arrayX.length; i++) {
  arrayX[i] = random();
}

```

Then you cannot index out of arrays size. Indexing of arrays start from 0 and last index is `length-1`

Have a look at reference for more examples [https://processing.org/reference/Array.html](https://processing.org/reference/Array.html)

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 25, 2020, 12:25pm UTC](https://discourse.processing.org/t/array-out-of-range-exeption-into-school-project/20133/3 "2020-04-25T12:25:14Z")

</div>

please show your code, so we can find the error

e.g. post the crucial nested for loops

---

<div class="post-metadata">

**Author:** ![Jestsect](https://avatars.discourse-cdn.com/v4/letter/j/e47774/32.png) [@Jestsect](https://discourse.processing.org/u/Jestsect)\
**Post date:** [April 25, 2020, 1:16pm UTC](https://discourse.processing.org/t/array-out-of-range-exeption-into-school-project/20133/6 "2020-04-25T13:16:05Z")

</div>

There are two more small parts of the code which are about menu but you could need it if want to run the game, tell me if you want it.

I’ m sorry if it isn’t really clear, I don’t know make it easier to read, if I can do annythig tell me and thanks a lot for your help.

---

<div class="post-metadata">

**Author:** ![Jestsect](https://avatars.discourse-cdn.com/v4/letter/j/e47774/32.png) [@Jestsect](https://discourse.processing.org/u/Jestsect)\
**Post date:** [April 25, 2020, 1:24pm UTC](https://discourse.processing.org/t/array-out-of-range-exeption-into-school-project/20133/7 "2020-04-25T13:24:25Z")

</div>

here is my collision code

```auto
 FloatList listeXenemi = new FloatList(); // listeX est la liste des abscisses des enemis. Au départ, cette liste est vide. 
FloatList listeYenemi = new FloatList();
 FloatList listeXshots = new FloatList(); // listeX est la liste des abscisses des balles. Au départ, cette liste est vide. 
FloatList listeYshots = new FloatList();

void collision(){
  for (int i =0 ; i<listeXenemi.size() ; i++) {
      for (int j =0 ; j<listeXshots.size() ; j++) {

     if ((dist(listeXenemi.get(i),listeYenemi.get(i),listeXshots.get(j),listeYshots.get(j)) < 50))      
      {
     
      listeXenemi.remove(i);
      listeYenemi.remove(i);
      listeXshots.remove(j);
      listeYshots.remove(j);
   }
  }
}
}

```

I have posted all few min ago but it was impossible to read, I don’t know if you can see deleted coments so here is the most important part

I also delete things which were out of the screen

---

<div class="post-metadata">

**Author:** ![Schred](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/schred/32/13146_2.png) [@Schred](https://discourse.processing.org/u/Schred)\
**Post date:** [April 25, 2020, 1:34pm UTC](https://discourse.processing.org/t/array-out-of-range-exeption-into-school-project/20133/9 "2020-04-25T13:34:11Z")

</div>

Loop through the lists backwards:

```auto
void collision() {
  for (int i = listeXenemi.size() - 1; i >= 0; i--) {
    for (int j = listeXshots.size() - 1; j >= 0; j--) {
      if ((dist(listeXenemi.get(i), listeYenemi.get(i), listeXshots.get(j), listeYshots.get(j)) < 50)) {
        listeXenemi.remove(i);
        listeYenemi.remove(i);
        listeXshots.remove(j);
        listeYshots.remove(j);

        break; // This is needed so you don't keep checking for an enemy that doesn't even exist anymore
      }
    }
  }
}

```

You have to do this whenever you’re removing from a list in a loop.

---

<div class="post-metadata">

**Author:** ![Jestsect](https://avatars.discourse-cdn.com/v4/letter/j/e47774/32.png) [@Jestsect](https://discourse.processing.org/u/Jestsect)\
**Post date:** [April 25, 2020, 2:37pm UTC](https://discourse.processing.org/t/array-out-of-range-exeption-into-school-project/20133/10 "2020-04-25T14:37:11Z")

</div>

Thanks a lot, now it work !
