ITCC 005 | Loops & Arrays


Today’s Artists


See the separate page on loops (for and while)


For now, all of our variables have contained just a single value. But computers are really good at processing a lot of data so it’s useful to understand of a way to store multiple values of the same type. Arrays allow you to do that. In many ways they are like the variables we have used before, they have a data type, name, value(s), and scope. In addition, arrays also have a size (or length as it is called in Processing). The length/size defines how many different values the array stores.

If we previously imagined a variable as a box, think of the array as box really large box that has multiple sections in it, or as a pile of boxes.

See the Processing reference for the following:

And read through the Array Tutorial from Processing.

Arrays can be created in multiple ways, but one key thing to understand is that in Processing, you always have to define the size of the array when you create it.

You can either directly initialize the array with some values like this:

int[] myArray = {4, 8, 2, 6, 5};
// this would create an array with a length of 5

Or you can create an empty array of a certain size/length:

float[] lotsOfNumbers = new float[100];
// this would create an array with a length of 100

Accessing the values in an array is done with the array access operator [].

The array access operator is used to specify a location within an array. The data at this location can be defined with the syntax array[element] = value and read with the syntax value = array[element] as shown in the example below.

int[] numbers = new int[3]; 
numbers[0] = 90; 
numbers[1] = 150; 
numbers[2] = 30; 
int a = numbers[0] + numbers[1]; // Sets variable 'a' to 240 
int b = numbers[1] + numbers[2]; // Sets variable 'b' to 180 

Arrays go really well together with the for loops, since you usually need to go through all the elements in an array one by one. The for loop structure is ideal for this.

int[] myArray = {4, 8, 2, 6, 5};

for (int i = 0; i < myArray.length; i++) {
  println(i + ": " + myArray[i]);


Random Walker With One Element

Random Walkers with Arrays

Random Walkers with More Arrays