# Problem with adding objects to ArrayList

**URL:** https://discourse.processing.org/t/problem-with-adding-objects-to-arraylist/8764
**Category:** Coding Questions
**Created:** [February 27, 2019, 12:55pm UTC](https://discourse.processing.org/t/problem-with-adding-objects-to-arraylist/8764 "2019-02-27T12:55:21Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Tymek](https://avatars.discourse-cdn.com/v4/letter/t/b487fb/32.png) [@Tymek](https://discourse.processing.org/u/Tymek)
#### Post date: [February 27, 2019, 12:55pm UTC](https://discourse.processing.org/t/problem-with-adding-objects-to-arraylist/8764/1 "2019-02-27T12:55:21Z")

</div>

Hello,

I wonder how to add object to ArrayList, when some conditions have been fullfiled. For example, i want to add new cell only if my mouse coursor is over one of each cells. Code is here. Thanks in advance 🙂

```auto
//Mithosis Simulation
ArrayList<Cell> cell;

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

  cell = new ArrayList<Cell>();

  cell.add(new Cell());
}

void draw() {
  background(0);

  for (Cell c : cell) {
    c.overlap();
    c.show();
    //c.Add();
  }
}

void mousePressed() {
  for (Cell c : cell) {
    if (c.over) {
      cell.add(new Cell());
    }
  }
}

class Cell {
  float xpos, ypos;
  int r, g, b;
  float rad;
  float d;
  boolean over = false;
  PVector acc;
  PVector vel;

  Cell() {
    this.rad = 15;
    this.xpos = random(rad, width-rad);
    this.ypos = random(rad, height-rad);
    this.r = 255;
    this.g = 0;
    this.b = 200;
    this.vel = new PVector(0, 0);
    this.acc = new PVector(0, 0);
  }

  void show() {
    noStroke();
    fill(r, g, b, 100);
    circle(xpos, ypos, 2*rad);
  }

  void overlap() {

    d = sqrt( pow((xpos-mouseX), 2) + pow((ypos-mouseY), 2) );

    if (d <= rad) {
      r = 0;
      g = 255;
      b = 200;
      over = true;
    } else {
      r = 255;
      g = 0;
      b = 200;
      over = false;
    }
  }

  void devide() {
  }

  void Add() {

    if (mousePressed && over) {
      cell.add(new Cell());
    }
  }
}

```

---

<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: [February 27, 2019, 2:10pm UTC](https://discourse.processing.org/t/problem-with-adding-objects-to-arraylist/8764/2 "2019-02-27T14:10:17Z")

</div>

`if (c.over) {` ➡ `if (c.overlap()) {`

> **[Collision Detection](http://JeffreyThompson.org/collision-detection/point-circle.php)**
>
> An online book about collision detection using Processing.

---

<div class="post-metadata">

### Author: ![Tymek](https://avatars.discourse-cdn.com/v4/letter/t/b487fb/32.png) [@Tymek](https://discourse.processing.org/u/Tymek)
#### Post date: [February 27, 2019, 3:27pm UTC](https://discourse.processing.org/t/problem-with-adding-objects-to-arraylist/8764/3 "2019-02-27T15:27:39Z")

</div>

I dont get it 😕 Still got this error “ConcurrentModificationException” while i click on the cell

---

<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: [February 27, 2019, 5:27pm UTC](https://discourse.processing.org/t/problem-with-adding-objects-to-arraylist/8764/4 "2019-02-27T17:27:48Z")

</div>

> [@Tymek](#):
>
> ConcurrentModificationException

A container being iterated by an “enhanced” `for ( : )` loop can’t change it’s **size()**. ⛔

Use a vanilla `for ( ; ; )` loop instead: 💡

```auto
for (int len = cell.size(), i = 0; i < len; ++i)
  if (cell.get(i).overlap()) cell.add(new Cell());

```

> **[for / Reference](https://processing.org/reference/for.html)**
>
> Controls a sequence of repetitions. A basic for structure has three parts: init, test, and update. Each part must be separated by a semicolon (;). The loop continues until …
