Arrays
An array is a variable that can hold multiple values of the same type.
In the first statement, the array a
is initialized to contain four elements.
The variable a
is of type int[],
which represents an array of integers.
The values contained in an array are called elements of the array.
Each element of an array acts just as an ordinary variable.
The example program shows how to create an array, access its elements,
and assign new values to them.
Each element can be accessed and manipulated individually
using its index in square brackets.
The elements of an array can be accessed using the index operator.
The index operator is [], as shown in the example program.
The argument of the index operator [] is called an index.
In the case of arrays, an index is an expression which must always be of type
int .
Remember that the index must always be an integer value,
which is why the last line is commented out - attempting to use a non-integer value as an index
would result in an error.
The getter .n returns the number of elements in an array.
A for loop can be used to iterate over all elements of an array.
The loop variable can be used as an index to access each element.
Note that the ++ operator is used in the
for loop to ensure that the loop counts upwards.
The potential issue is that an array can be empty, which would cause the getter
.n to return zero, and the loop to try iterating backwards.
This program creates an array a containing three integers.
It then attempts to print four elements from the array a.
Since the array only has three elements, trying to access the fourth element
results in a runtime error.
The elements of an array can be of any type, including built-in types like
num, int, and str.
In this example, the array a has elements of type
num,
as indicated by the syntax num[].
Note that the indices of an array are always of type int,
regardless of the type of the elements.
Therefore, in order to access an element, the type of expression for the index
must always be int.
The elements of an array can be of type color.
In this example, the for loop is used to draw a series of discs,
each with a color from the array.
Note that, in the disc procedure call, the color value is selected
from the array by an index operator.
Exercise:
Modify the for loop to count backwards, so that the discs are drawn in reverse order.
The elements of an array can be of type string.
Exercise:
Modify the program to print the number of characters in each line of the song,
followed by the line itself.
For example:
29: When the blazing sun is gone,
Solution
:println songLyrics[i].n, ": ", songLyrics[i]
This program uses an array of five points.
The array is named centers.
Then it draws discs at the positions given by the array centers.
To see the coordinates of each disc printed out,
uncomment the line starting with /// .
Exercise (Challenging!):
Modify the program to paint each disc in a different color,
using labeled initializers to define the colors.
(The solution is provided on the next page.)
To draw discs in various colors, this program introduces an array of colors.
The array is named colors.
The discp function now takes
an additional argument, colors[i],
which specifies the color of each disc.
By using the same index i to access
both the centers and colors arrays,
the program ensures that each disc is drawn with the corresponding color.
This program also moves the discs by the motion vector mv.
To see the position and the motion vector of each disc,
uncomment the lines starting with ///.
Exercise:
Make each disc move in a different direction.
The motion vector for each disc should be specified by a different unlabeled initializer.
(The solution is provided on the next page.)
This program uses a distinct motion vector for each disc.
The motion vectors are given by the mv array.
The product of the motion vector and the execution time execTm()
scales the motion vectors, creating the illusion of movement as the displacement from the disc centers grows over time.