# How to erase an object inside of an array?

**URL:** <https://discourse.processing.org/t/how-to-erase-an-object-inside-of-an-array/28499>\
**Category:** Beginners\
**Created:** [March 13, 2021, 2:00pm UTC](https://discourse.processing.org/t/how-to-erase-an-object-inside-of-an-array/28499 "2021-03-13T14:00:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [March 13, 2021, 2:00pm UTC](https://discourse.processing.org/t/how-to-erase-an-object-inside-of-an-array/28499/1 "2021-03-13T14:00:24Z")

</div>

Lets say I have an ArrayList of a class of objects. How would I be able to do erase it from within the class. Something like

```auto
ArrayList<newClass> newClassList = new ArrayList<newClass>();
newClassList.add(newClass());
class newClass {
   float x, y;
   void getInfo(float x_, float y_) { x = x_; y = y_; }
   void erase() {
       //here comes the line that erases the object.
   }
}

```

I know it could be done by comparing itself to all the other objects in the array, but still.  
Essentially I want a function that erases the object from the ArrayList.

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [March 13, 2021, 2:46pm UTC](https://discourse.processing.org/t/how-to-erase-an-object-inside-of-an-array/28499/2 "2021-03-13T14:46:47Z")

</div>

Hi,

First of all, when you say erase in reality it’s the garbage collector that does it automatically in Java. In lower level languages like C, you can explicitly delete some piece of memory that was previously allocated.

So in this case, you rather remove the object from the array and then later it’s going to be free by the garbage collector.

Now if you want to do this inside a method, you need to tell the method from which ArrayList you want to remove it so pass it as argument :

```auto
class NewClass {
  void eraseFrom(ArrayList<NewClass> array) {
    array.remove(this);
  }
}

```

Few notes here :

- Always use PascalCase for class names in Java (see the [conventions](https://www.oracle.com/java/technologies/javase/codeconventions-namingconventions.html) )

- It’s better to choose method names that actually describe what they do (so erase from something…)

- It’s using the [ArrayList.remove(Object o)](https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#remove(java.lang.Object)) method to remove an object from the array. It does that :

Anyway for me it’s more clear to do this the other way around :

```auto
ArrayList<NewClass> newClassList = new ArrayList<NewClass>();

NewClass instance = new NewClass();

newClassList.add(instance);

// Do stuff...

newClassList.remove(instance);

```
