# Simple Iterative List

**URL:** https://discourse.processing.org/t/simple-iterative-list/37398
**Category:** Beginners
**Created:** [June 8, 2022, 7:52am UTC](https://discourse.processing.org/t/simple-iterative-list/37398 "2022-06-08T07:52:26Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![panko](https://avatars.discourse-cdn.com/v4/letter/p/c5a1d2/32.png) [@panko](https://discourse.processing.org/u/panko)
#### Post date: [June 8, 2022, 7:52am UTC](https://discourse.processing.org/t/simple-iterative-list/37398/1 "2022-06-08T07:52:26Z")

</div>

Hi there -

I am doing something a few times in a row. I feel like there must be a better way to write this. Can anyone help me?

Thank you!

```auto
PShape square1, square2, square3; 

void setup() {
  size(300, 300);
  // Creating the PShape as a square. The
  // numeric arguments are similar to rect().
  square1 = createShape(RECT, random(200), random(200), random(50+100), random(50+100));
  square1.setFill(color(random(255), 0, random(255)));
  
  square2 = createShape(RECT, random(200), random(200), random(50+100), random(50+100));
  square2.setFill(color(random(255), 0, random(255)));
  
  square3 = createShape(RECT, random(200), random(200), random(50+100), random(50+100));
  square3.setFill(color(random(255), 0, random(255)));
}

void draw() {
  shape(square1);
  shape(square2);
  shape(square3);

}

```

---

<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: [June 8, 2022, 7:53am UTC](https://discourse.processing.org/t/simple-iterative-list/37398/2 "2022-06-08T07:53:41Z")

</div>

Yeah, an Array. Check this out: [Reference / Processing.org](https://processing.org/reference/Array.html)

Tutorial: [Arrays / Processing.org](https://processing.org/tutorials/arrays)

**Example**

Array with a for-loop in draw

```auto
PShape[] squares = new PShape[3]; 

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

  // Creating the PShape as a square. The
  // numeric arguments are similar to rect().
  squares[0] = createShape(RECT, random(200), random(200), random(50+100), random(50+100));
  squares[0].setFill(color(random(255), 0, random(255)));

  squares[1] = createShape(RECT, random(200), random(200), random(50+100), random(50+100));
  squares[1].setFill(color(random(255), 0, random(255)));

  squares[2] = createShape(RECT, random(200), random(200), random(50+100), random(50+100));
  squares[2].setFill(color(random(255), 0, random(255)));
}

void draw() {
  // and a for loop, that takes each square from the list
  for ( PShape currentSquare : squares) {
    shape(currentSquare);
  }
}

```

* * *

**Second Example**

Of course, you also can make a for loop in setup() to make it even shorter

```auto
PShape[] squares = new PShape[3]; 

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

  // we use a for loop to fill the slots of the array 
  for (int i=0; i<squares.length; i++) {
    // Creating the PShape as a square. The numeric arguments are similar to rect().
    squares[i]= createShape(RECT, random(200), random(200), random(50+100), random(50+100));
    squares[i].setFill(color(random(255), 0, random(255)));
  }
}

void draw() {
  // and a for loop that takes each square from the list
  for (PShape currentSquare : squares) {
    shape(currentSquare);
  }
}

```

- unfortunately, the nice form of a for-loop as in draw() cannot be used in setup() since the array is empty in setup() and we first have to fill it.
