# Updating ArrayList with additional instances of object

**URL:** https://discourse.processing.org/t/updating-arraylist-with-additional-instances-of-object/38849
**Category:** Coding Questions
**Created:** [September 19, 2022, 1:35am UTC](https://discourse.processing.org/t/updating-arraylist-with-additional-instances-of-object/38849 "2022-09-19T01:35:52Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)
#### Post date: [September 19, 2022, 1:35am UTC](https://discourse.processing.org/t/updating-arraylist-with-additional-instances-of-object/38849/1 "2022-09-19T01:35:52Z")

</div>

Hello!  
I’ve think I’ve made _some_ progress but…

The goal is to:

- loop through an ArrayList which displays as a grid, – completed
- randomly select a few of the cells, – completed
- subdivide the selected into 4 smaller cells, – completed
- add the new cells to the ArrayList – **problem area**
- display the new grid that has the larger and smaller GridCells – **problem area**

I guess the main problem is I’m not clear on how to access and then integrate the smaller cells into the main ArrayList.

Any guidance is greatly appreciated!  
🤓

//////////////////////////////////////////////////////////////////////////////////////////  
Current version of code as follows:

```auto
import java.util.List;
List<GridCell>cell = new ArrayList<GridCell>();

void setup() {
  size (600, 600);
  background(255);
  noLoop();

  for (int x = 0; x < width; x+= 100) {
    for (int y = 0; y < height; y+= 100) {
      cell.add(new GridCell(x, y, 100, 100));
    }
  }
}

void draw() {

  for (GridCell c : cell) {
    c.display();
  }
  subdivideCells();
}

void subdivideCells() {
  List<GridCell>fourCells = new ArrayList<GridCell>();
  
  for (int i = 0; i < cell.size(); i++) {
    if (random(1)<0.5)
       cell.get(i).subdivide();  
    cell.add(fourCells); //***this is wrong but I don't know why
  }
}
/////////////////////////////////////////////////////////////////////
class GridCell {
  int x, y, w, h;

  GridCell(int x_, int y_, int w_, int h_) {
    x = x_;
    y = y_;
    w = w_;
    h = h_;
  }

  void display() {
    stroke(0);
    strokeWeight(2);
    rect(x, y, w, h);
  }

  List<GridCell>subdivide() {
    List<GridCell> newCells = new ArrayList();

    newCells.add(new GridCell(x, y, w/2, h/2));
    newCells.add(new GridCell(x+w/2, y, w/2, h/2));
    newCells.add(new GridCell(x, y+h/2, w/2, h/2));
    newCells.add(new GridCell(x+w/2, y+h/2, w/2, h/2));
    
    return newCells;
  }
}

```

---

<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: [September 19, 2022, 1:42am UTC](https://discourse.processing.org/t/updating-arraylist-with-additional-instances-of-object/38849/2 "2022-09-19T01:42:24Z")

</div>

> [@debxyz](#):
>
> `if (random(1)<0.5)`

Firstly, here a block with {…} should start because you already have TWO lines that are dependent upon the if clause not only one.

Second: the function returns a new arraylist. You need to receive this arraylist from the function (like `List<GridCell> newCells = ...subdivide...`). This is where you call the function. Then for loop over the new small arraylist and add the items to the main arraylist. This is all inside said {…}

---

<div class="post-metadata">

### Author: ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)
#### Post date: [September 19, 2022, 2:01am UTC](https://discourse.processing.org/t/updating-arraylist-with-additional-instances-of-object/38849/3 "2022-09-19T02:01:20Z")

</div>

Hello @Chrisir !  
Thank you for the quick reply!  
I made 2 changes based on your response, but  
See below:

```auto
void subdivideCells() {

  for (int i = 0; i < cell.size(); i++) {
    
    List<GridCell>fourCells = new ArrayList<GridCell>(); // move the List to here?

    if (random(1)<0.5) { //added the missing {...}
      cell.get(i).subdivide();  
      cell.add(fourCells);
    }
  }
}

```

> [@Chrisir](#):
>
> (like ‘’‘List newCells = …subdivide…’‘’). This is where you call the function.

This is the part where I’m most confused. I can’t figure out what I’m supposed to be connecting and in what order…but will look at this some more…  
🤔

---

<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: [September 19, 2022, 2:55am UTC](https://discourse.processing.org/t/updating-arraylist-with-additional-instances-of-object/38849/5 "2022-09-19T02:55:14Z")

</div>

> [@debxyz](#):
>
> can’t figure out what I’m supposed to be connecting and in what order

I am talking about this line:

`cell.get(i).subdivide();`

just put the `List....=` before it

Then for loop over the result list

---

<div class="post-metadata">

### Author: ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)
#### Post date: [September 19, 2022, 3:32am UTC](https://discourse.processing.org/t/updating-arraylist-with-additional-instances-of-object/38849/6 "2022-09-19T03:32:22Z")

</div>

I think this is getting closer?  
But consistent error message on the last line…

```auto
void subdivideCells() {

  for (int i = 0; i < cell.size(); i++) {

    if (random(1)<0.5) {
      List<GridCell> newCells = cell.get(i).subdivide(); // Yes?? no error message here now
      //for (int i = 0; i < newCells.size(); i++) { //replaced regular for loop with for each to eliminate duplicate i variable
      for (GridCell g : newCells) {

        cell.add(newCells); //keep getting error message on this line
      }
    }
  }

```

---

<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: [September 19, 2022, 3:46am UTC](https://discourse.processing.org/t/updating-arraylist-with-additional-instances-of-object/38849/7 "2022-09-19T03:46:29Z")

</div>

> [@debxyz](#):
>
> `cell.add(newCells); //keep getting error message `

cell.add(g);

Chrisir

---

<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: [September 19, 2022, 3:48am UTC](https://discourse.processing.org/t/updating-arraylist-with-additional-instances-of-object/38849/8 "2022-09-19T03:48:37Z")

</div>

Consider to remove the old cell after you divided it

Which is actually very complicated and should be avoided

---

<div class="post-metadata">

### Author: ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)
#### Post date: [September 19, 2022, 4:17am UTC](https://discourse.processing.org/t/updating-arraylist-with-additional-instances-of-object/38849/9 "2022-09-19T04:17:30Z")

</div>

> [@Chrisir](#):
>
> cell.add(g);

Thank you!!! That cleared the last error message.  
Now the program hangs when I try to run it. Got some time out error messages so not sure what is happening. I’ll try again tomorrow.  
Thank you again!!!  
I greatly appreciate the help.  
🤓

---

<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: [September 19, 2022, 4:18am UTC](https://discourse.processing.org/t/updating-arraylist-with-additional-instances-of-object/38849/10 "2022-09-19T04:18:57Z")

</div>

When you think about it it’s actually kind of recursive

So just far too many cells are created

---

<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: [September 19, 2022, 4:20am UTC](https://discourse.processing.org/t/updating-arraylist-with-additional-instances-of-object/38849/11 "2022-09-19T04:20:15Z")

</div>

> [@debxyz](#):
>
> ```auto
> subdivideCells();
> 
> ```

Call this in draw() only 4 times max

Or call it in setup 4 times

---

<div class="post-metadata">

### Author: ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)
#### Post date: [September 19, 2022, 4:31am UTC](https://discourse.processing.org/t/updating-arraylist-with-additional-instances-of-object/38849/12 "2022-09-19T04:31:51Z")

</div>

> [@Chrisir](#):
>
> Call this in draw() only 4 times max
> 
> Or call it in setup 4 times

Yes, I had thought of trying that. 🙂  
Right now, am trying to get a better handle on manipulating arrayLists so probably won’t attempt removing old cells at this time (or maybe ever… 🙃

---

<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: [September 19, 2022, 6:55am UTC](https://discourse.processing.org/t/updating-arraylist-with-additional-instances-of-object/38849/13 "2022-09-19T06:55:32Z")

</div>

> [@debxyz](#):
>
> ```auto
> for (int i = 0; i < cell.size(); i++) {
> 
> ```

Just occurred to me that might also be dangerous because we add cells, so that the size grows. Then the for loop  
would never stop

I’ll look into this

---

<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: [September 19, 2022, 11:33am UTC](https://discourse.processing.org/t/updating-arraylist-with-additional-instances-of-object/38849/14 "2022-09-19T11:33:44Z")

</div>

I looked into this, it’s more complicate than I thought it was

```auto
ArrayList<GridCell> cells = new ArrayList<GridCell>();

void setup() {
  size (600, 600);
  background(255);
  noLoop();

  // make grid
  for (int x = 0; x < width; x+= 100) {
    for (int y = 0; y < height; y+= 100) {
      cells.add(new GridCell(x, y, 100, 100));
    }
  }

  // divide cells 
  for (int i = 0; i < 16; i++) {
    subdivideCells();
  }
}

void draw() {
  // display grid 
  for (GridCell c : cells) {
    c.display();
  }
}

//-----------------------------------------------------------------------------------------------------
// Other function 

void subdivideCells() {

  // It is necessary to have a controlCounter and use this is in the for-loop since we change the size() of the ArrayList
  int controlCounter=0; 

  // It is necessary to store upperBound and use this is in the for-loop since we change the size() of the ArrayList
  int upperBound = cells.size(); 
  // println(cells.size() +"\n--------------------" ); 

  for (int i = 0; i < upperBound; i++) {
    if (random(1)<0.5) {
      // get this cell from the ArrayList
      GridCell parentCell = cells.get(i);
      // mark this parent cell as dead
      parentCell.isDead=true; 
      // get 4 sub rectangles "newCells" and add them to the main list "cells"
      ArrayList<GridCell> newCells = parentCell.subdivide();
      // replaced regular for loop with for each  
      for (GridCell currentNewCell : newCells) {
        cells.add(currentNewCell);
      }//for
    }//if

    controlCounter++;
    if (controlCounter>1784) {
      println("abort "+controlCounter);
      return;
    }//if
  }//for

  // remove dead cells (backward)
  for (int i = cells.size()-1; i > 0; i--) {
    if (cells.get(i).isDead) {
      cells.remove(i);
    }
  }
}//func 

/////////////////////////////////////////////////////////////////////

class GridCell {

  int x, y, 
    w, h;

  color colGridCell = color(random(255), random(255), random(255)); 

  boolean isDead=false;

  GridCell(int x_, int y_, int w_, int h_) {
    x = x_;
    y = y_;
    w = w_;
    h = h_;
  }

  void display() {
    stroke(0);
    // noStroke(); 
    // strokeWeight(2);
    fill(colGridCell); 
    rect(x, y, w, h);
  }

  ArrayList<GridCell>subdivide() {
    ArrayList<GridCell> newCells = new ArrayList();

    newCells.add(new GridCell(x, y, w/2, h/2));
    newCells.add(new GridCell(x+w/2, y, w/2, h/2));
    newCells.add(new GridCell(x, y+h/2, w/2, h/2));
    newCells.add(new GridCell(x+w/2, y+h/2, w/2, h/2));

    return newCells;
  }//method
  //
}//class
//

```

---

<div class="post-metadata">

### Author: ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)
#### Post date: [September 19, 2022, 11:32pm UTC](https://discourse.processing.org/t/updating-arraylist-with-additional-instances-of-object/38849/15 "2022-09-19T23:32:41Z")

</div>

@Chrisir Thank you so very much!!! This is greatly helpful!! 😂

I understand all of the code and logic.

But one small question about the control counter.  
Code runs the same with the controlCounters commented out.  
**Is this supposed to be linked to the cells.size()?**  
I’m probably overlooking something…

> [@Chrisir](#):
>
> ```auto
> // It is necessary to have a controlCounter and use this is in the for-loop since we change the size() of the ArrayList
> int controlCounter=0; 
> 
> ```

> [@Chrisir](#):
>
> ```auto
> controlCounter++;
> if (controlCounter>1784) { // 1784? Where does this number come from?
> println("abort "+controlCounter);
> return;
> 
> ```

---

<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: [September 20, 2022, 1:19am UTC](https://discourse.processing.org/t/updating-arraylist-with-additional-instances-of-object/38849/16 "2022-09-20T01:19:18Z")

</div>

> [@Chrisir](#):
>
> `controlCounter`

I had the controlCounter to end the for loop early.

In my tests the image looked best when it was around 1700; when it was 17000 the image had to many black areas, probably due to the black outline stroke color of the rectangles

---

<div class="post-metadata">

### Author: ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)
#### Post date: [September 20, 2022, 1:32am UTC](https://discourse.processing.org/t/updating-arraylist-with-additional-instances-of-object/38849/17 "2022-09-20T01:32:23Z")

</div>

Ok, that makes sense  
Thank you again!  
🤓
