# Simple Iterative List

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!

``````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);

}
``````

Yeah, an Array. Check this out: Reference / Processing.org

Tutorial: Arrays / Processing.org

Example

Array with a for-loop in draw

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

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

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

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

squares = createShape(RECT, random(200), random(200), random(50+100), random(50+100));
squares.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

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

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.
5 Likes