# Is it a good way to empty an arrayList?

**URL:** <https://discourse.processing.org/t/is-it-a-good-way-to-empty-an-arraylist/43167>\
**Category:** Beginners\
**Created:** [November 8, 2023, 12:02am UTC](https://discourse.processing.org/t/is-it-a-good-way-to-empty-an-arraylist/43167 "2023-11-08T00:02:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![amundsen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/amundsen/32/2497_2.png) [@amundsen](https://discourse.processing.org/u/amundsen)\
**Post date:** [November 8, 2023, 12:02am UTC](https://discourse.processing.org/t/is-it-a-good-way-to-empty-an-arraylist/43167/1 "2023-11-08T00:02:34Z")

</div>

Hello,

I use an arrayList to store points making a polyline and I want to replace the points each time I press the mouse button.

Instead of using a for loop to iterate through the arrayList and remove the items one by one using the remove method I recreate the arrayList instead. Is it a good practice or not?

```auto
int numPoints = 10;

 ArrayList<Point> MesPoints = new ArrayList();

void setup() {
  size(900, 900);
  background(0);
}

void draw() {
  background(0);
  stroke(255,0,0);
  strokeWeight(5);
  for (int i = 0; i < MesPoints.size()-1; i++) {
    Point oldp = MesPoints.get(i);
    Point newp = MesPoints.get(i+1);
    line(oldp.pos.x, oldp.pos.y, newp.pos.x, newp.pos.y);
  }

}

void mousePressed() {
  MesPoints = new ArrayList(); // important! Recreation of the arraylist to clean it!
  for (int i = 0; i < numPoints ; i++) {
    Point np=new Point();
    np.pos=new PVector(random(width), random(height));
    MesPoints.add(np);
  }
}

class Point {
  PVector pos; 
}

```

---

<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:** [November 8, 2023, 12:09am UTC](https://discourse.processing.org/t/is-it-a-good-way-to-empty-an-arraylist/43167/2 "2023-11-08T00:09:32Z")

</div>

Hello @amundsen,

> **[Clear ArrayList with clear() vs. removeAll()](https://howtodoinjava.com/java/collections/arraylist/empty-clear-arraylist/)**
>
> Learn to clear arraylist or empty an arraylist in Java. Clearing a list means to remove all elements from the list. Difference between clear and removeAll.

`:)`

---

<div class="post-metadata">

**Author:** ![amundsen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/amundsen/32/2497_2.png) [@amundsen](https://discourse.processing.org/u/amundsen)\
**Post date:** [November 8, 2023, 12:16am UTC](https://discourse.processing.org/t/is-it-a-good-way-to-empty-an-arraylist/43167/3 "2023-11-08T00:16:13Z")

</div>

Haha. Great @glv! But I wonder if recreating the arrayList is more efficient or not as both clear() and removeAll() imply some iteration whereas a full recreation doesn’t I guess.

---

<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:** [November 8, 2023, 12:29am UTC](https://discourse.processing.org/t/is-it-a-good-way-to-empty-an-arraylist/43167/4 "2023-11-08T00:29:58Z")

</div>

Related topic here:

> <https://stackoverflow.com/questions/6961356/list-clear-vs-list-new-arraylistinteger>

`:)`

---

<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:** [November 8, 2023, 4:18am UTC](https://discourse.processing.org/t/is-it-a-good-way-to-empty-an-arraylist/43167/5 "2023-11-08T04:18:25Z")

</div>

B/c I prefer to declare my variables w/ keyword `final` as much as possible I only use **clear()** for it:

> **[final / Reference](https://processing.org/reference/final.html)**
>
> Keyword used to state that a value, class, or method can't be changed. If the final keyword is used to define a variable, the variable can't be changed within the program. When used to define a…

```auto
import java.util.List;

static final int DOTS = 10;
final List<PVector> dots = new ArrayList<PVector>(DOTS);

void setup() {
  size(800, 600);
  noLoop();

  mousePressed();

  stroke(#FF0000);
  strokeWeight(5);
}

void draw() {
  background(0);
  
  int idx = 0;
  PVector pos = dots.get(idx);
  float x = pos.x, y = pos.y;

  while (++idx < DOTS) {
    pos = dots.get(idx);
    line(x, y, x = pos.x, y = pos.y);
  }
}

void keyPressed() {
  mousePressed();
}

void mousePressed() {
  dots.clear();

  for (int i = 0; i < DOTS; ++i) {
    dots.add(new PVector(random(width), random(height)));
  }

  redraw();
}

```

Main advantage of keeping around the same container is to spare the garbage collector from dealing w/ all discarded previous containers.

However, giving the container is always **size()** = DOTS, we don’t really need method **clear()**.

We can simply refresh each PVector w/ randomized new values via method **set()**:

> **[set() / Reference](https://processing.org/reference/PVector_set_.html)**
>
> Sets the x, y, and z component of the vector using two or three separate variables, the data from a PVector, or the values from a float array.

```auto
import java.util.List;

static final int DOTS = 10;
final List<PVector> dots = new ArrayList<PVector>(DOTS);

void setup() {
  size(800, 600);
  noLoop();

  for (int i = 0; i < DOTS; ++i) dots.add(new PVector());
  mousePressed();

  stroke(#FF0000);
  strokeWeight(5);
}

void draw() {
  background(0);

  int idx = 0;
  PVector pos = dots.get(idx);
  float x = pos.x, y = pos.y;

  while (++idx < DOTS) {
    pos = dots.get(idx);
    line(x, y, x = pos.x, y = pos.y);
  }
}

void keyPressed() {
  mousePressed();
}

void mousePressed() {
  for (final PVector pos : dots) pos.set(random(width), random(height));
  redraw();
}

```

Actually, we can simplify it further by just using a regular Java array for such cases:

> **[Array / Reference](https://processing.org/reference/Array.html)**
>
> An array is a list of data. It is possible to have an array of any type of data. Each piece of data in an array is identified by an index number representing its position in the array. The first eleme…

```auto
static final int DOTS = 10;
final PVector[] dots = new PVector[DOTS];

void setup() {
  size(800, 600);
  noLoop();

  for (int i = 0; i < DOTS; dots[i++] = new PVector());
  mousePressed();

  stroke(#FF0000);
  strokeWeight(5);
}

void draw() {
  background(0);

  int idx = 0;
  PVector pos = dots[idx];
  float x = pos.x, y = pos.y;

  while (++idx < DOTS) {
    pos = dots[idx];
    line(x, y, x = pos.x, y = pos.y);
  }
}

void keyPressed() {
  mousePressed();
}

void mousePressed() {
  for (final PVector pos : dots) pos.set(random(width), random(height));
  redraw();
}

```
