# ArrayList out of boundary

**URL:** <https://discourse.processing.org/t/arraylist-out-of-boundary/22817>\
**Category:** Beginners\
**Created:** [July 24, 2020, 1:30pm UTC](https://discourse.processing.org/t/arraylist-out-of-boundary/22817 "2020-07-24T13:30:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![klap](https://avatars.discourse-cdn.com/v4/letter/k/a698b9/32.png) [@klap](https://discourse.processing.org/u/klap)\
**Post date:** [July 24, 2020, 1:30pm UTC](https://discourse.processing.org/t/arraylist-out-of-boundary/22817/1 "2020-07-24T13:30:05Z")

</div>

Hi,  
Playing with objects and ArrayList.

I’ve trouble removing objects from the ArrayList when they disappear from the screen.  
IndexOutOfBoundsException: Index: 49, Size: 49

```auto
ArrayList<Bubble> bubble = new ArrayList<Bubble>();

//Bubble bubble;
float speed;
float dia;
float x, y;
int numberB = 50;

void setup() {
  size(400, 400, P2D);

  y = height- (0.5 * dia);

  for (int i = numberB -1; i >= 0; i--) {
    x = random(dia, width-dia);
    speed = random(0.2, 1.2);
    dia = random(5, 30);
    bubble.add(new Bubble(x, y, dia, speed));
  }
  //bubble = new Bubble(random(10, 50), random(1,3));
}

void draw() {
  background(0);
  for (int i = numberB -1; i >= 0; i--) {

    Bubble bub = bubble.get(i);
    bub.display();
    bub.move();
    if (bub.mode == 1) {
      bubble.remove(i);
      println("removed");
    }
    //bubble.get(i).disappear(i);
  }
}

class Bubble {
  float x, y, diameter, yspeed;
  int mode = 0;

  Bubble(float tempX, float tempY, float tempDiameter, float tempYspeed) {

    diameter = tempDiameter;
    yspeed = tempYspeed;
    x = tempX;
    y = tempY;
  }

  void display() {
    //fill(255);
    noFill();
    stroke(255);
    ellipse(x, y, diameter, diameter);
  }

  void move() {
    
    y -= yspeed;
    if (y + dia < 0) {
     mode = 1;
    println("mode = 1");
    //}
  }
  }

  boolean disappear() {
    if (y + dia < 0) {
      return true;
    } else {
      return false;
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [July 24, 2020, 1:48pm UTC](https://discourse.processing.org/t/arraylist-out-of-boundary/22817/2 "2020-07-24T13:48:14Z")

</div>

Try:

```auto
for (int i = bubble.size() -1; i >= 0; i--)

```

Give it some thought and you will understand why.

`:)`

---

<div class="post-metadata">

**Author:** ![klap](https://avatars.discourse-cdn.com/v4/letter/k/a698b9/32.png) [@klap](https://discourse.processing.org/u/klap)\
**Post date:** [July 26, 2020, 9:21am UTC](https://discourse.processing.org/t/arraylist-out-of-boundary/22817/3 "2020-07-26T09:21:42Z")

</div>

I like the way you answer with a question 😉

I assume it’s because bubble.size() is flexible, where the variable numberB is fixed and so when a item is removed from the ArrayList, numberB should change, like is now done with bubble.size().

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [July 26, 2020, 2:19pm UTC](https://discourse.processing.org/t/arraylist-out-of-boundary/22817/4 "2020-07-26T14:19:33Z")

</div>

Hello,

References for you to explore:  
[https://processing.org/reference/ArrayList.html](https://processing.org/reference/ArrayList.html)  
[Googe Search for ArrayList Processing](https://www.google.com/search?q=codingtrain+arraylist&rlz=1C1CHBF_enCA822CA822&oq=codingtrain+arraylist&aqs=chrome..69i57.14670j0j8&sourceid=chrome&ie=UTF-8)

`:)`
