# How to delete an object in a class?

**URL:** <https://discourse.processing.org/t/how-to-delete-an-object-in-a-class/19067>\
**Category:** Coding Questions\
**Created:** [March 27, 2020, 2:14am UTC](https://discourse.processing.org/t/how-to-delete-an-object-in-a-class/19067 "2020-03-27T02:14:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cybernet](https://avatars.discourse-cdn.com/v4/letter/c/54ee81/32.png) [@Cybernet](https://discourse.processing.org/u/Cybernet)\
**Post date:** [March 27, 2020, 2:14am UTC](https://discourse.processing.org/t/how-to-delete-an-object-in-a-class/19067/1 "2020-03-27T02:14:55Z")

</div>

So I have a class that is a bullet. And if the bullet is out of bound, then it will delete the object. But how do I do this? Here is my code.

```auto
class Bullet
{
    if (xpos > 500 || ypos > 500)
        delete object // how do I do this part?
}

```

As you can see, I want to delete the object if it goes out of bounds so I don’t have 10000000 bullets in my game. How do I do this? Thanks

---

<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:** [March 27, 2020, 8:58am UTC](https://discourse.processing.org/t/how-to-delete-an-object-in-a-class/19067/2 "2020-03-27T08:58:50Z")

</div>

It depends from how you use the bullet.

Are they in an Arraylist? Use remove here.

Or in an array?

I think you can outside the class say bullet = null; but the you need an if-clause to check the bullet for null before using it

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [March 27, 2020, 10:43am UTC](https://discourse.processing.org/t/how-to-delete-an-object-in-a-class/19067/3 "2020-03-27T10:43:48Z")

</div>

This sketch demonstrates how to do this. It starts by creating 200 bullets with randon position and speed and stores them in an array list. When a bullet moves off the screen it is considered dead and is removed from the list. The number of remaining bullets is shown.

```auto
int n = 200;

void setup() {
  size(640, 480);
  textSize(48);
  for (int i = 0; i < n; i++) {
    bullets.add(new Bullet());
  }
}

void draw() {
  background(0);
  updateBullets();
  for (Bullet b : bullets) {
    b.render();
  }
  fill(255, 200, 255, 160);
  text("" + bullets.size(), 80, 80, 200, 120);
}

public void updateBullets() {
  for (Bullet b : bullets) {
    b.move();
  }
  // Remove dead bullets.
  // Start at end of list to avoid concurrent modification exception
  for (int i = bullets.size() - 1; i >= 0; i--) {
    if (!bullets.get(i).isAlive()) { // in other words dead
      bullets.remove(i);
    }
  }
}

public class Bullet {
  float x;
  float y;
  float vx;
  float vy;
  boolean alive = true;

  public Bullet() {
    x = random(30, width -30);
    y = random(30, height -30);
    vx = random(-0.5, 0.5);
    vy = random(-0.5, 0.5);
  }

  public void move() {
    x += vx;
    y += vy;
    // Still alive (true) if still on screen
    alive = (x >= 0) && (x < width) && (y >= 0) && (y < height);
  }

  public void render() {
    noStroke();
    fill(0, 255, 0);
    pushMatrix();
    translate(x, y);
    ellipse(0, 0, 6, 6);
    popMatrix();
  }

  public boolean isAlive() {
    return alive;
  }
}
``
```

---

<div class="post-metadata">

**Author:** ![Cybernet](https://avatars.discourse-cdn.com/v4/letter/c/54ee81/32.png) [@Cybernet](https://discourse.processing.org/u/Cybernet)\
**Post date:** [March 27, 2020, 4:19pm UTC](https://discourse.processing.org/t/how-to-delete-an-object-in-a-class/19067/4 "2020-03-27T16:19:39Z")

</div>

Thank you. Could you also explain what concurrent modification exception is?

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [March 27, 2020, 5:02pm UTC](https://discourse.processing.org/t/how-to-delete-an-object-in-a-class/19067/5 "2020-03-27T17:02:20Z")

</div>

If we look the code to remove dead bullets we are are doing two things

1. we are iterating over the list (visiting each bullet object in the list) with the for-loop
2. inside the for-loop we are removing bullets from the list, hence modifying the list

The problem is that under certain circumstance, doing both these things together i.e. concurrently causes an exception. For instance if you replace the code with this

```auto
  // Remove dead bullets.
  // Start at beginning of the list 
  for (int i = 0; i < bullets.size(); i--) {
    if (!bullets.get(i).isAlive()) { // in other words dead
      bullets.remove(i);
    }
  }

```

_Semantically_ this code does the same as before but because of the way Java handles lists it will cause the exception.

If you want a better way to avoid the exception read on but it involves more advanced concepts. ⚠

===================================================================

Generally it is not recommended to write code based on knowledge of how Java works under the hood because the implementation details might be changed in the future and invalidate the code. In this particular instance the implementation of the Java ArrayList is never likely to change so it is quite safe to use the code in my previous reply.

For those interested the totally safe way to do this based on the Java API is

```auto
public void updateBullets() {
  Iterator<Bullet> iter = bullets.iterator();
  while (iter.hasNext()) {
    Bullet b = iter.next();
    b.move();
    if (!b.isAlive()) {
      iter.remove();
    }
  }
}

```

Would also need to add the import statement  
`import java.util.*`

---

<div class="post-metadata">

**Author:** ![Cybernet](https://avatars.discourse-cdn.com/v4/letter/c/54ee81/32.png) [@Cybernet](https://discourse.processing.org/u/Cybernet)\
**Post date:** [March 27, 2020, 5:14pm UTC](https://discourse.processing.org/t/how-to-delete-an-object-in-a-class/19067/6 "2020-03-27T17:14:36Z")

</div>

Oh okay thank you. What about Vector? Can I use that too?

---

<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:** [March 27, 2020, 5:18pm UTC](https://discourse.processing.org/t/how-to-delete-an-object-in-a-class/19067/7 "2020-03-27T17:18:10Z")

</div>

- [Problem regarding ArrayList - Processing 2.x and 3.x Forum](http://Forum.Processing.org/two/discussion/22678/problem-regarding-arraylist#Item_9)

> [@Help with a short game code](https://discourse.processing.org/t/help-with-a-short-game-code/11108/2):
>
> We can’t use method remove() when using an “enhanced” for ( : ) {} style! no_entry_sign Use a vanilla for ( ; ; ) {} style instead. Preferably, w/ backwards iteration: back

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [March 27, 2020, 5:32pm UTC](https://discourse.processing.org/t/how-to-delete-an-object-in-a-class/19067/8 "2020-03-27T17:32:15Z")

</div>

The `Vector` class has the same API as the `ArrayList` so yes you can use it instead. I should point out that the `Vector` class is synchronized (thread safe) so might be slower than `ArrayList`. Since your code is single threaded you don’t need a synchronized class so I would stick with `ArrayList` unless there is a good reason not to.
