2. arrays
• Using variables , we can store information at one step in the program
for use at a later time. We store a single value per variable. Arrays now
allow us to cache multiple values at once. An array is a list of
variables where each element has the same data type . An array is a
data structure.
3. Declaration of an array
• Arrays must be declared like variables before they can be used, except that there are square brackets after the data type:
// Declaration of an integer variable
int number;
// declaration of an integer array
int [] numbers;
To initialize an array, we need to define the length of the array . The length determines how many elements can be stored in the array.
In the following example we define our array numbers so that five elements (of type int) can be stored in it, and the other array positionen so that we float
can store 30 elements, i.e. 30 values.
// Array with five integer elements
int [] numbers = new int [5];
// Array of thirty point number elements
float [] positions = new float [30];
An array is initialized like this: new data type[length]; so for example new float[4];
4. Access to individual items
• We can access each item through an index . This index is always an integer and starts with 0 . Each element
is of the same data type and can be used like a normal variable. When we access a single element of the array,
we can read and write its value.
• Here we see how to assign new values to the elements of an array
int x = 5;
int [] numbers = new int [5];
numbers[0] = 100;
numbers[1] = 255;
numbers[2] = 2;
numbers[3] = width ;
numbers[4] = 10 * x;
5. Access to individual items
• We can use the individual elements as well as variables:
int x = new numbers[4];
int y = 4;
y = y + numbers[3];
line (x, y, numbers[1], numbers[2]);
• And of course we can also use a variable or expressions for the index:
int number = 10;
// Be it initialization
char [] character = new char [number];
// Or when accessed
int i = 5;
numbers[i] = numbers[2 + 1];
6. Arrays & Loops
• When using or computing a lot of similar data, arrays are very useful.
• For example, if we wanted to animate a ball, we had to store its
position and speed in variables: x, y, vx, vy. If not just one but several
balls are to be displayed, you would have to use four variables per ball
( x2, y2, vx2, vy2, x3, y3, ...), which would be very tedious. Instead, we
can use arrays.
• So the purpose of arrays is that we can store many of the same kind
of variables together — and save us a lot of unnecessary typing and
programming.
7. Using arrays
• In order to access each element, we can either specify each position
individually ( ), or access all of them numbers[3] using a for loop .
• We can then use the counter variable of a for loop directly as an
index:
int [] numbers = new int [5];
for ( int i = 0; i < 5; i++) {
numbers[i] = i * 10;
}
8. Example:
draws ten circles using the values of two arrays as positions.
float [] x = new float [10];
float [] y = new float [10];
Void setup ( ) {
size (200, 200);
// Initialize all elements of both arrays
for ( int i = 0; i < 10; i++) {
x[i] = random ( width );
y[i] = random ( height );
}
}
void draw () {
// Draw circles using a loop and the arrays
for ( int i = 0; i < 10; i++) {
ellipse (x[i], y[i], 20, 20);
}
]