# How to remove a specific object from an Arraylist

**URL:** https://discourse.processing.org/t/how-to-remove-a-specific-object-from-an-arraylist/24599
**Category:** Coding Questions
**Tags:** homework
**Created:** [October 14, 2020, 2:54pm UTC](https://discourse.processing.org/t/how-to-remove-a-specific-object-from-an-arraylist/24599 "2020-10-14T14:54:47Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Ohli](https://avatars.discourse-cdn.com/v4/letter/o/a3d4f5/32.png) [@Ohli](https://discourse.processing.org/u/Ohli)
#### Post date: [October 14, 2020, 2:54pm UTC](https://discourse.processing.org/t/how-to-remove-a-specific-object-from-an-arraylist/24599/1 "2020-10-14T14:54:47Z")

</div>

I am working on a project for a beginner programming course. I am making a simple game where a vacuum character needs to collect small brown circles (rubbish) by moving over them and using a suction device, whilst avoiding moving spikey balls.

My character moves using the mouse and uses his suction device when a key is pressed. I have an array of rubbish set up but I have been struggling to figure out how to remove a specific one when a key is pressed and the mouse/character collides with it. I would also like this to count up a score displayed on screen but this is not a priority.

I have pasted my code below, sorry if it is really messy, I am not very good at this.

TLDR: I want to remove a specific object from an Array when the player has a key pressed and their mouse is colliding with it.

```auto
ArrayList<Rubbish> rubbishArray = new ArrayList<Rubbish>();
Spikeyball1 s1;
Spikeyball1 s2;
int x = 0;
int y = 0;
int p = 0;
int borderY = 300;
int gameScreen = 0;

void setup() {
  size( 1600, 1000 );

  s1 = new Spikeyball1(500, 300);
  s2 = new Spikeyball1(1100, 1000);

  for (int i = 0; i < 10; i++) {
    rubbishArray.add(new Rubbish());
  }
}

void draw() {

  if (gameScreen == 0) {
    initScreen();
  } else if (gameScreen == 1) {
    gameScreen();
  } else if (gameScreen == 2) {
    gameOverScreen();
  }
}

void initScreen() {

  background(0);
  textAlign(CENTER);
  textSize(50);
  text("Click to Start", 800, 500);
}

void gameScreen() {

  environment();
  noCursor();

  if (rubbishArray.size() < 10) {
    rubbishArray.add(new Rubbish());
  }

  for (Rubbish r : rubbishArray) {
    r.move();
    r.display();
  }

  //top border
  if (mouseY<borderY) mouseY = borderY;

  //move vacuum
  if (keyPressed) p = 20;
  else p = 0;

  //spikeyballs
  s1.move();
  s1.display();
  s2.move();
  s2.display();
  s1.collision();
  s2.collision();

  //character
  vacuum(mouseX, mouseY+p);
  characterA(mouseX, mouseY);
}

void gameOverScreen() {

  background(210);
  textAlign(CENTER);
  textSize(50);
  text("Game Over!", 800, 400);
  text("Click to Restart", 800, 600);
}

void startGame() {
  gameScreen=1;
}

void mousePressed() {

  if (gameScreen==0) {
    startGame();
  }
  if (gameScreen==2) {
    startGame();
  }
}

void characterA(float x, float y) {

  stroke( 0 );
  strokeWeight( 2 );

  fill(0, 0, 102); 
  ellipse(x-25, y+10, 15, 15);
  ellipse(x-8, y+10, 15, 15);
  ellipse(x+9, y+10, 15, 15);
  ellipse(x+26, y+10, 15, 15);
  fill(0, 76, 153); 
  quad(x+5, y-73, x+8, y-103, x+18, y-100, x+15, y-70);
  quad(x-15, y-70, x-18, y-100, x-8, y-103, x-5, y-73);
  fill(0, 76, 153); 
  rect(x-35, y-45, 70, 50);
  fill(0, 100, 153); 
  arc(x, y-45, 70, 60, PI, TWO_PI);
  fill(255); 
  ellipse(x-13, y-100, 15, 15);
  ellipse(x+13, y-100, 15, 15);
  fill(0); 
  ellipse(x-13, y-100, 5, 5);
  ellipse(x+13, y-100, 5, 5);
  noFill(); 
  arc(x, y-65, 20, 20, 0, PI);
}

void vacuum(float x, float y) { 

  stroke( 0 );
  strokeWeight( 2 );

  fill(0, 100, 153); 
  quad(x+25, y-30, x+35, y-20, x+65, y-50, x+55, y-60);
  fill(0, 76, 153); 
  quad(x+45, y-50, x+75, y-20, x+85, y-30, x+55, y-60);
  fill(0, 100, 153); 
  triangle(x+56, y-13, x+96, y-13, x+76, y-43);
}

void environment() {

  fill(150); 
  quad(-500, 300, -200, 300, 100, 1000, -200, 1000);
  fill(125); 
  quad(-200, 300, 100, 1000, 400, 1000, 100, 300);
  fill(150); 
  quad(400, 1000, 100, 300, 400, 300, 700, 1000);
  fill(125); 
  quad(400, 300, 700, 1000, 1000, 1000, 700, 300);
  fill(150); 
  quad(700, 300, 1000, 1000, 1300, 1000, 1000, 300);
  fill(125); 
  quad(1000, 300, 1300, 1000, 1600, 1000, 1300, 300);
  fill(150); 
  quad(1300, 300, 1600, 1000, 1900, 1000, 1600, 300);
  fill(220); 
  rect(-10, 0, 1650, 100);
  fill(200); 
  rect(-10, 100, 1650, 100);
  fill(220); 
  rect(-10, 200, 1650, 100);
  fill(10); 
  rect(1000, 100, 300, 100);
  beginShape();
  fill(240);
  vertex(300, 300);
  vertex(450, 300);
  vertex(450, 0);
  vertex(350, 0);
  vertex(300, 100);
  endShape(CLOSE);
  beginShape();
  vertex(600, 300);
  vertex(450, 300);
  vertex(450, 0);
  vertex(550, 0);
  vertex(600, 100);
  endShape(CLOSE);
}

class Rubbish {
  float x;
  float y;
  float speed = random(2, 5);
  float r = random(80, 150);
  float g = random(45, 55);
  float b = 0;

  Rubbish() {
    x = 0;
    y = random(400, 900);
  }

  void move() {
    x+= speed;
    if (x>1600) {
      x=0;
    }
  }

  void display() {
    fill(r, g, b); 
    ellipse(x, y, 30, 30);
  }
}

class Spikeyball1 {

  float xpos;
  float ypos;
  float r;
  boolean movingUp = true;
  int m = 0;
  boolean collision;

  Spikeyball1(float tempXpos, float tempYpos) {
    xpos = tempXpos;
    ypos = tempYpos;
  }

  void move() {

    if (movingUp) ypos += 3;
    else ypos -= 3;
    if (ypos<300||ypos>1000) movingUp = !movingUp;
  }

  void collision() {

    if ( mouseX >= xpos-50 && mouseX <= xpos+50 && mouseY >= ypos-50 && mouseY <= ypos+100) {
      gameScreen = 2;
    };
  }

  void display() {

    stroke( 0 );
    strokeWeight( 2 );

    fill(60, 1, 1); 
    beginShape();
    vertex(xpos, ypos-50);
    vertex(xpos+14, ypos-20);
    vertex(xpos+47, ypos-15);
    vertex(xpos+23, ypos+7);
    vertex(xpos+29, ypos+40);
    vertex(xpos, ypos+25);
    vertex(xpos-29, ypos+40);
    vertex(xpos-23, ypos+7);
    vertex(xpos-47, ypos-15);
    vertex(xpos-14, ypos-20);
    endShape(CLOSE);
    fill(200, 20, 10); 
    ellipse(xpos, ypos, 50, 50);
    fill(255); 
    ellipse(xpos+8, ypos-5, 10, 10);
    ellipse(xpos-8, ypos-5, 10, 10);
    fill(0); 
    ellipse(xpos+8, ypos-5, 3, 3);
    ellipse(xpos-8, ypos-5, 3, 3);
    noFill(); 
    arc(xpos, ypos+15, 25, 20, PI, TWO_PI);
  }
}

```

---

<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: [October 14, 2020, 3:25pm UTC](https://discourse.processing.org/t/how-to-remove-a-specific-object-from-an-arraylist/24599/2 "2020-10-14T15:25:45Z")

</div>

list.remove(i)

When you need to remove multiple items, use a for loop which must go backward

---

<div class="post-metadata">

### Author: ![Ohli](https://avatars.discourse-cdn.com/v4/letter/o/a3d4f5/32.png) [@Ohli](https://discourse.processing.org/u/Ohli)
#### Post date: [October 14, 2020, 4:46pm UTC](https://discourse.processing.org/t/how-to-remove-a-specific-object-from-an-arraylist/24599/3 "2020-10-14T16:46:29Z")

</div>

I am not sure how to apply this. I know that I can remove specific objects from an array, but how do I tell it that (i) is the object colliding with the mouse.

Also I don’t think I can use list.remove(i) in Java, please correct me if I am wrong.

---

<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: [October 14, 2020, 5:19pm UTC](https://discourse.processing.org/t/how-to-remove-a-specific-object-from-an-arraylist/24599/4 "2020-10-14T17:19:25Z")

</div>

> [@Ohli](#):
>
> Also I don’t think I can use list.remove(i) in Java, please correct me if I am wrong.

_[ArrayList / Reference / Processing.org](https://processing.org/reference/ArrayList.html)_

`:)`

---

<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: [October 14, 2020, 5:29pm UTC](https://discourse.processing.org/t/how-to-remove-a-specific-object-from-an-arraylist/24599/5 "2020-10-14T17:29:45Z")

</div>

> [@Chrisir](#):
>
> , use a for loop which must go backward.

> [@Table removeColumn(index) not working correctly?](https://discourse.processing.org/t/table-removecolumn-index-not-working-correctly/17585/4):
>
> For completeness’ sake, although I prefer backwards back loops for deleting elements, here’s how to do the same w/ a forward arrow_forward loop the right way: smile_cat final IntList nums = IntList.fromRange(10); println(nums); println("Forwards:"); for(int i = 0; i \< nums.size(); ++i) { println("index:", i, TAB, "value:", nums.get(i)); nums.remove(i--); } println(nums); exit(); B/c we’re emptying the whole container, the index i is stuck at value 0 on all iterations. open_mouth …

---

<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: [October 15, 2020, 8:12am UTC](https://discourse.processing.org/t/how-to-remove-a-specific-object-from-an-arraylist/24599/6 "2020-10-15T08:12:47Z")

</div>

> [@Ohli](#):
>
> how do I tell it that (i) is the object colliding with the mouse.

That’s right!

My answer was a bit too short.

For example you for loop over all rubbish:

When the Distance to the mouse is \< 18 (use `dist()` command) set a marker `boolean isDead` (which is in the class) to true (initially in the class say `boolean isDead=false;`).

**Remove from the ArrayList**

Now later in draw() you for-loop over the rubbish backwards and check isDead:

```auto
for(int i=list.size()-1; i >= 0; i--) { 
    Rubbish item=list.get(i); ` 
    if (item.isDead) 
        list.remove(i);
}

```

Chrisir

---

<div class="post-metadata">

### Author: ![Ohli](https://avatars.discourse-cdn.com/v4/letter/o/a3d4f5/32.png) [@Ohli](https://discourse.processing.org/u/Ohli)
#### Post date: [October 15, 2020, 9:02am UTC](https://discourse.processing.org/t/how-to-remove-a-specific-object-from-an-arraylist/24599/7 "2020-10-15T09:02:09Z")

</div>

Thank you so much! I got it working 😃

I had one more question though, how would I go about making it so that each item removed adds to a score that I display on screen?

---

<div class="post-metadata">

### Author: ![Ohli](https://avatars.discourse-cdn.com/v4/letter/o/a3d4f5/32.png) [@Ohli](https://discourse.processing.org/u/Ohli)
#### Post date: [October 15, 2020, 9:24am UTC](https://discourse.processing.org/t/how-to-remove-a-specific-object-from-an-arraylist/24599/8 "2020-10-15T09:24:08Z")

</div>

Nevermind I managed to implement a score all by myself xD Thanks again This was giving me a headache for ages!

---

<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: [October 15, 2020, 10:48am UTC](https://discourse.processing.org/t/how-to-remove-a-specific-object-from-an-arraylist/24599/9 "2020-10-15T10:48:43Z")

</div>

I am glad to hear this!

Congratulations!

Chrisir
