# Creating a function that returns an ArrayList of manipulated objects

**URL:** <https://discourse.processing.org/t/creating-a-function-that-returns-an-arraylist-of-manipulated-objects/38824>\
**Category:** Coding Questions\
**Created:** [September 17, 2022, 4:24pm UTC](https://discourse.processing.org/t/creating-a-function-that-returns-an-arraylist-of-manipulated-objects/38824 "2022-09-17T16:24:37Z")\
**Posts on this page:** 7\
**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 17, 2022, 4:24pm UTC](https://discourse.processing.org/t/creating-a-function-that-returns-an-arraylist-of-manipulated-objects/38824/1 "2022-09-17T16:24:37Z")

</div>

Hello!!

I have been trying to figure out the syntax to create a method that will subdivide a GridCell object.

The GridCells display in the main program in a 2-D grid.  
In the main program I want to be able to loop thru the grid, select a GridCell, and divide that cell into 4 new smaller cells.  
I have picked up _some_ info via Coding Train, stack overflow, and myriad other sites. However reaching a point in the code below.

I have read/watched content that discusses **passing a reference vs passing a value** , in addition to methods that return arrayLists. I think this will somehow affect how to write the syntax but am not sure how to integrate this into the code.

Additionally, since a function can only return a single value/reference—and I need to return 4 separate values/referneces—I have placed 4 functions (one for each value to be returned) into a function that would return an ArrayLIst of the 4 values returned. **Is this a valid approach?**

The syntax(and maybe even the logic) below is probably laughably wrong…but hopefully it illustrates the intent.

Any guidance is gratefully appreciated.  
🤓

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

```auto
class GridCell{
// variables & constructor of a simple GridCell w/ x, y, w, h coordinates...

void display(){
// rect(x, y, w, h);
}

// **here is the question:**
// **I know this syntax is not correct**
public ArrayList subdivide (Arraylist GridCell smCell) {

    GridCell topLeft() {
      return new GridCell(smCell.x, smCell.y, w/2, h/2);
    }
    GridCell topRight() {
      return new GridCell(smCell.x+w/2, smCell.y, w/2, h/2);
    }
    GridCell lowerLeft() {
      return new GridCell(smCell.x, smCell.y+h/2, w/2, h/2);
    }
    GridCell lowerRight() {
      return new GridCell(smCell.x+w/2, smCell.y+h/2, w/2, h/2);
    }
    return ArrayList GridCell smCell;
  }

```

---

<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:** [September 17, 2022, 4:39pm UTC](https://discourse.processing.org/t/creating-a-function-that-returns-an-arraylist-of-manipulated-objects/38824/2 "2022-09-17T16:39:11Z")

</div>

Hi @debxyz,

Nice question! 😉

> [@debxyz](#):
>
> I have placed 4 functions (one for each value to be returned) into a function that would return an ArrayLIst of the 4 values returned. **Is this a valid approach?**

This is almost right but as you wrote, your syntax is not correct. The idea is to have a method that returns a list of 4 cells by subdividing the current cell.

You can do the following:

```processing
import java.util.List;

class GridCell {
  int x, y, w, h;
  
  GridCell(int x, int y, int w, int h) {
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;
  }
  
  List<GridCell> subdivide() {
    List<GridCell> cells = new ArrayList();
    
    cells.add(new GridCell(x, y, w / 2, h / 2));
    cells.add(new GridCell(x + w / 2, y, w / 2, h / 2));
    cells.add(new GridCell(x, y + h / 2, w / 2, h / 2));
    cells.add(new GridCell(x + w / 2, y + h / 2, w / 2, h / 2));
    
    return cells;
  }
  
  String toString() {
    return "(" + x + ", " + y + ", " + w + ", " + h + ")";
  }
}

GridCell cell = new GridCell(50, 50, 200, 200);
println(cell.subdivide());

```

Some notes here:

- Noticed how I used the `List` generic class instead of `ArrayList`. Since `ArrayList` is a class inheriting from `List`, it’s always preferable to use the most generic type when returning values or using them. (see: [java - Depend on abstractions. Do not depend on concrete classes - Stack Overflow](https://stackoverflow.com/questions/13552413/depend-on-abstractions-do-not-depend-on-concrete-classes))

- The `toString()` method is a special method when implemented on classes that is used when an object is converted to String. For example when printing it with `println()`

Hope it helps!

---

<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 17, 2022, 5:32pm UTC](https://discourse.processing.org/t/creating-a-function-that-returns-an-arraylist-of-manipulated-objects/38824/3 "2022-09-17T17:32:49Z")

</div>

Thank you @josephh !!  
This does indeed help! 😂  
I wasn’t sure if I could use dot syntax within a class… For some reason thought I could implement only in a main program… Glad to see I wasn’t totally out in left field.

> [@josephh](#):
>
> Noticed how I used the `List` generic class instead of `ArrayList`.

I sure did!!

Thank you again!!  
🤓

---

<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:** [September 17, 2022, 5:59pm UTC](https://discourse.processing.org/t/creating-a-function-that-returns-an-arraylist-of-manipulated-objects/38824/4 "2022-09-17T17:59:21Z")

</div>

No idea if it’s even correct, but this is my attempt on furthering @josephh’s example: 🤪

```auto
// Dividable GridCell (v1.0.5) (2022/Sep/17)

// https://Discourse.Processing.org/t/
// creating-a-function-that-returns-an-arraylist-of-manipulated-objects/38824/4

import java.util.List;
final List<Cell> cells = new ArrayList<Cell>();

int curSize;

void setup() {
  size(1200, 900);
  clear();
  noLoop();
  strokeWeight(4);

  cells.add(new Cell(50, 50, width - 100, height - 100));
}

void draw() {
  for (final Cell cell : cells) {
    stroke((color) random(#000000));
    fill((color) random(#000000));
    cell.display();
  }

  println(cells);
  println(cells.size(), ENTER);
  surface.setTitle("Frame: " + frameCount + " - Cells: " + cells.size());

  subdivideAllCells();
}

void mousePressed() {
  redraw();
}

void subdivideAllCells() {
  for (int i = 0, len = cells.size(); i < len; ++i) if ((curSize -= 4) < 0)
    for (final Cell c : cells.get(i).subdivide()) cells.add(c);

  curSize = cells.size();
}

class Cell {
  int x, y, w, h;

  Cell(int xx, int yy, int ww, int hh) {
    x = xx;
    y = yy;
    w = ww;
    h = hh;
  }

  Cell display() {
    rect(x, y, w, h);
    return this;
  }

  Cell[] subdivide() {
    final int ww = w >> 1, hh = h >> 1;

    return new Cell[] {
      new Cell(x, y, ww, hh), // top-left
      new Cell(x + ww, y, ww, hh), // top-right
      new Cell(x, y + hh, ww, hh), // bottom=left
      new Cell(x + ww, y + hh, ww, hh) // bottom-right
    };
  }

  String toString() {
    return "(" + x + ", " + y + ", " + w + ", " + h + ")";
  }
}

```

P.S.: After adding variable _curSize_ as a way to skip Cell objects which had already **subdivide()**, this [ArrayList](https://processing.org/reference/ArrayList.html) version now behaves very similar to the other I did relying instead on a [LinkedHashSet](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/LinkedHashSet.html).

---

<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:** [September 17, 2022, 10:44pm UTC](https://discourse.processing.org/t/creating-a-function-that-returns-an-arraylist-of-manipulated-objects/38824/5 "2022-09-17T22:44:34Z")

</div>

A new approach using a [LinkedHashSet](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/LinkedHashSet.html) container in order to avoid duplicate GridCell objects. #️⃣

```auto
// Dividable GridCell II (v1.0.4) (2022/Sep/17)

// https://Discourse.Processing.org/t/
// creating-a-function-that-returns-an-arraylist-of-manipulated-objects/38824/5

import java.awt.Rectangle;

import java.util.Set;
import java.util.LinkedHashSet;
final Set<Cell> cells = new LinkedHashSet<Cell>();

import java.util.List;
final List<Cell> newBornCells = new ArrayList<Cell>();

int curSize;

void setup() {
  size(1200, 900);
  clear();
  noLoop();
  strokeWeight(4);

  cells.add(new Cell(50, 50, width - 100, height - 100));
}

void draw() {
  for (final Cell cell : cells) cell.display();

  println(cells);
  println(cells.size(), ENTER);
  surface.setTitle("Frame: " + frameCount + " - Cells: " + cells.size());

  subdivideAllCells();
}

void mousePressed() {
  redraw();
}

void subdivideAllCells() {
  newBornCells.clear();

  for (final Cell cell : cells) if ((curSize -= 4) < 0)
    for (final Cell c : cell.subdivide()) newBornCells.add(c);

  cells.addAll(newBornCells);
  curSize = cells.size();
}

class Cell extends Rectangle {
  color paint, border;

  Cell(int x, int y, int w, int h) {
    super(x, y, w, h);

    paint = color((color) random(#000000));
    border = color((color) random(#000000));
  }

  Cell display() {
    fill(paint);
    stroke(border);
    rect(x, y, width, height);
    return this;
  }

  Cell[] subdivide() {
    final int ww = width >> 1, hh = height >> 1;

    return new Cell[] {
      new Cell(x, y, ww, hh), // top-left
      new Cell(x + ww, y, ww, hh), // top-right
      new Cell(x, y + hh, ww, hh), // bottom=left
      new Cell(x + ww, y + hh, ww, hh) // bottom-right
    };
  }

  String toString() {
    return "(" + x + ", " + y + ", " + width + ", " + height + ")";
  }
}

```

In this new version class Cell `extends` [Rectangle](https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/java/awt/Rectangle.html) in order to inherit its **equals()** & **hashCode()** methods so container [LinkedHashSet](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/LinkedHashSet.html) can tell apart 1 from the other based on their dimensions. 💡

---

<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 18, 2022, 7:27pm UTC](https://discourse.processing.org/t/creating-a-function-that-returns-an-arraylist-of-manipulated-objects/38824/6 "2022-09-18T19:27:51Z")

</div>

@GoToLoop This is VERY interesting!!  
First time to see bitwise shifting and I appreciate the introduction. 😀

Plus the:

> [@GoToLoop](#):
>
> After adding variable _curSize_ as a way to skip Cell objects which had already **subdivide()**,

is super cool!! I will spend some time studying this.  
Thank you!!!  
🤓

---

<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, 8:00pm UTC](https://discourse.processing.org/t/creating-a-function-that-returns-an-arraylist-of-manipulated-objects/38824/7 "2022-09-19T20:00:03Z")

</div>

continued here [Updating ArrayList with additional instances of object](https://discourse.processing.org/t/updating-arraylist-with-additional-instances-of-object/38849)
