# Array Index Out of Bounds error on Array List

**URL:** https://discourse.processing.org/t/array-index-out-of-bounds-error-on-array-list/3006
**Category:** Processing
**Created:** [August 27, 2018, 2:11am UTC](https://discourse.processing.org/t/array-index-out-of-bounds-error-on-array-list/3006 "2018-08-27T02:11:21Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![eschulzpsd](https://avatars.discourse-cdn.com/v4/letter/e/a5b964/32.png) [@eschulzpsd](https://discourse.processing.org/u/eschulzpsd)
#### Post date: [August 27, 2018, 2:11am UTC](https://discourse.processing.org/t/array-index-out-of-bounds-error-on-array-list/3006/1 "2018-08-27T02:11:21Z")

</div>

I’m creating a random item generator from an array list. When I get down to an empty list, I want a ‘gameOver’ function to run, but I get an Array Index Out of Bounds error. I’m wondering if I have a variable issue when the list is empty. I’m posting all the code here (sorry, first post, don’t know if there’s a better way to do this):

```auto
StringList names;
String [] list = {"apple","orange","grape","banana","lemon"};
String item;
int s = 1;
int i;
int x;

void setup(){
  size(300,300);
  names = new StringList(list);
  textAlign(CENTER);
  textSize(60);
  fill(255);
  background(0);
}

void randomSelect(){
  int s = names.size();
  print("(" + s + ") ");
  println(list);
  int x = int(random(s));
  item = names.get(x);
  println("Name Removed: " + item);
  fill(random(255),random(255),random(255));
  text(item, width/2,height/2);
  names.remove(x);
  s = s - 1;
  String[] newList = names.array();
  list = newList;
  print("(" + s + ") ");
  println(list);
  println();
}

void mouseClicked(){
  background(0);
  if(s > 0){
    randomSelect();
  }else{
    gameOver();
  }
}

void gameOver(){
  background(0);
  textSize(35);
  text("Out of Names", width/2,height/2);
}

void draw(){ 
}

```

---

<div class="post-metadata">

### Author: ![EnhancedLoop7](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/enhancedloop7/32/1040_2.png) [@EnhancedLoop7](https://discourse.processing.org/u/EnhancedLoop7)
#### Post date: [August 27, 2018, 2:28am UTC](https://discourse.processing.org/t/array-index-out-of-bounds-error-on-array-list/3006/2 "2018-08-27T02:28:13Z")

</div>

I’m sure that someone will tell you to format your code, you can do that by highlighting your code and pressing Control + Shift + C. What I did was add to your code:

```auto
if(names.size() > 0) 
 {
names.remove(x);
 }
if(names.size() == 0 )
 {
   gameOver();
 }

```

You’re getting an array index out of bound error, because when you remove the last item in your list, and then you try removing the object at the index of x, there is nothing to be removed. And then I just had an if statement say that if the size is zero, then to show your `gameOver()` function.

Hopefully this helps? If you have any more questions I would love to help, and please don’t mind keeping us updated on your project. (P.S. Do not forget to format your code next time 😃 )

EnhancedLoop7

---

<div class="post-metadata">

### Author: ![eschulzpsd](https://avatars.discourse-cdn.com/v4/letter/e/a5b964/32.png) [@eschulzpsd](https://discourse.processing.org/u/eschulzpsd)
#### Post date: [August 27, 2018, 2:50am UTC](https://discourse.processing.org/t/array-index-out-of-bounds-error-on-array-list/3006/3 "2018-08-27T02:50:23Z")

</div>

Thank you!! Super helpful. And thanks for the format help.

Next Q: How can I reset the array list to its original items with a mouse click?

Great thanks.

---

<div class="post-metadata">

### Author: ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)
#### Post date: [August 27, 2018, 5:58am UTC](https://discourse.processing.org/t/array-index-out-of-bounds-error-on-array-list/3006/4 "2018-08-27T05:58:52Z")

</div>

How about this:

you first create a global variable like this:

```auto
String[] originalList = {"apple", "orange", "grape", "banana", "lemon"};

```

Then you add a function that duplicate your list:

```auto
String[] duplicateOriginalList() {
  String[] result = new String[originalList.length];
  for(int i = 0; i < originalList.length; i++) {
    result[i] = originalList[i];
  }
  return result;
}

```

All you have to do then is add the following line in your `setup()` function and just after your `gameOver()` call.

```auto
list = duplicateOriginalList();

```

Probably not the best and the most efficient way to do it but it is quick to implement and you don’t have to redo all of your code 🙂

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [August 27, 2018, 10:35am UTC](https://discourse.processing.org/t/array-index-out-of-bounds-error-on-array-list/3006/5 "2018-08-27T10:35:21Z")

</div>

> [@jb4x](#):
>
> Then you add a function that duplicate your list:

```auto
String[] originalArray = { "apple", "orange", "grape", "banana", "lemon" };
String[] clonedArray = originalList.clone();

```

---

<div class="post-metadata">

### Author: ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)
#### Post date: [August 27, 2018, 11:29am UTC](https://discourse.processing.org/t/array-index-out-of-bounds-error-on-array-list/3006/6 "2018-08-27T11:29:12Z")

</div>

Hoooo that’s the one I was looking for! 🙂  
I was trying `.copy()` with no success… 🙄

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [August 27, 2018, 11:34am UTC](https://discourse.processing.org/t/array-index-out-of-bounds-error-on-array-list/3006/7 "2018-08-27T11:34:58Z")

</div>

And when the times comes to reset _clonedArray_ back to the contents of _originalArray_, use **arrayCopy()**: ©

`arrayCopy(originalArray, clonedArray);`

> **[arrayCopy() / Reference](https://processing.org/reference/arrayCopy_.html)**
>
> Copies an array (or part of an array) to another array. The src array is copied to the dst array, beginning at the position specified by srcPosition and into the position specifie…

Also be aware that such cloning & copying techniques are shallow! 😓  
And they should be used only for primitive and immutable datatypes! ⚠

[Docs.Oracle.com/javase/10/docs/api/java/lang/Object.html#clone()](http://Docs.Oracle.com/javase/10/docs/api/java/lang/Object.html#clone())

> Thus, this method performs a “shallow copy” of this object, not a “deep copy” operation.

---

<div class="post-metadata">

### Author: ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)
#### Post date: [August 27, 2018, 11:44am UTC](https://discourse.processing.org/t/array-index-out-of-bounds-error-on-array-list/3006/8 "2018-08-27T11:44:46Z")

</div>

> [@GoToLoop](#):
>
> Also be aware that such cloning technique is shallow!

Good to point out!  
I remember there was a thread last week about an implementation of the game of life where the behavior was weird because of that shallow copy.

---

<div class="post-metadata">

### Author: ![eschulzpsd](https://avatars.discourse-cdn.com/v4/letter/e/a5b964/32.png) [@eschulzpsd](https://discourse.processing.org/u/eschulzpsd)
#### Post date: [August 28, 2018, 3:19am UTC](https://discourse.processing.org/t/array-index-out-of-bounds-error-on-array-list/3006/9 "2018-08-28T03:19:11Z")

</div>

Great help. Clone works well, but I wasn’t able to make arrayCopy work. I did patch together a way to get it to restart. But it only runs one more time, so something is amiss.

```auto
StringList fruits;
StringList clones;
String [] list = {"apple","orange","grape","banana","lemon"};
String [] clonedList = list.clone();
String item;
int i;
int x;

void setup(){
  size(300,300);
  fruits = new StringList(list);
  clones = new StringList(clonedList);
  textAlign(CENTER);
  fill(255);
  background(0);
}

void randomSelect(){
  textSize(60);
  print("list(" + fruits.size() + ") ");
  println(list);
  print("clonedList(" + clones.size() + ") ");
  println(clonedList);
  int x = int(random(fruits.size()));
  item = fruits.get(x);
  println("Name Removed: " + item);
  fill(255,0,0);
  text(item, width/2,height/2);
  fruits.remove(x);
  String[] newList = fruits.array();
  list = newList;
  print("(" + fruits.size() + ") ");
  println(list);
  println();
}

void mouseClicked(){
  background(0);
  if(fruits.size() > 0){
    randomSelect();
  }else if(fruits.size() == 0){
    gameOver();
    list = clonedList;
    fruits = clones;
  }else if(list == clonedList){
    randomSelect();
  }
}

void gameOver(){
  background(0);
  textSize(35);
  fill(0,0,255);
  text("Out of Names", width/2,height/2);
}

void draw(){ 
}

```

---

<div class="post-metadata">

### Author: ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)
#### Post date: [August 28, 2018, 5:40am UTC](https://discourse.processing.org/t/array-index-out-of-bounds-error-on-array-list/3006/10 "2018-08-28T05:40:08Z")

</div>

You are making it sooo complicated 😄

Here something that is working with explanations. Hope it helps:

```auto
StringList originalFruits; // Will always contain the list of all the fruits
StringList fruits; // Will be the array that we use to get a new random fruit

void setup(){
  // Initializing both list
  originalFruits = new StringList("apple","orange","grape","banana","lemon");
  fruits = originalFruits.copy(); //It works because we are using string, It would't with custom classes
}

void randomSelect(){
  int idx = (int)random(fruits.size()); // Get a random index from 0 to the size of the array
  println(fruits.get(idx)); // Write in the console the name of the random picked fruit
  fruits.remove(idx); // Remove that fruit from the list since we dan't want to pick it again
}

void mouseClicked(){
  if(fruits.size() > 0) { // While there is still some fruit to pick we want to pick one
    randomSelect(); // So we pick one
  } else { // On the other case it means that we picked everything
    println("GAME OVER"); // So we are game over
    fruits = originalFruits.copy(); // And we need to fill the list of fruit again
  }
}

void draw(){ 
  
}

```

---

<div class="post-metadata">

### Author: ![eschulzpsd](https://avatars.discourse-cdn.com/v4/letter/e/a5b964/32.png) [@eschulzpsd](https://discourse.processing.org/u/eschulzpsd)
#### Post date: [August 28, 2018, 10:51pm UTC](https://discourse.processing.org/t/array-index-out-of-bounds-error-on-array-list/3006/11 "2018-08-28T22:51:25Z")

</div>

Thanks! That was super helpful! Less is more.😀
