Back

Arrays

Reading

Words form the thread on which we string our experiences. [Aldous Huxley]

Reference Variables in Memory

Before we declare an array, we need to understand the distinction between variables of the primitive type and variables of the reference type.

A variable of the primitive type simply points to a location in memory where a value is stored.

A reference variable points to the location of an object in memory, and that object stores pointers to each piece of information that it contains.

Since an array is an object, the variable for an array is a reference variable. This means that the variable itself does not represent a value, since an array is a collection of values it points to the collection.

We will see how to access the insectionidual values and variables held in the array in the coming sections.

Declaring Arrays

An array is a special datatype. Much like the string an array is a collection of elements. The difference is that an array is a collection of any type of element we specify. Let's take a look at declaring some arrays of different types.

int[] lottoNumbers = new int[5];
Declare array of integers, lottoNumbers, with a size of 5.
String[] names = new String[3];
Declare array of String Objects, names, with a size of 3.
boolean[] lights = new boolean[10];
Declare array of booleans, lights, with a size of 10.

Arrays are zero indexed like strings which means that even though the size or length may be 3 or 5, the actual position of the last element is always size - 1, since the positions start with 0. Arrays must specify their size when being declared, notice how every example states the number of elements they will contain.

Initializing Arrays

An array is useless unless each element is initialized. After declaring an array, we don't have any actual values in the positions in the array. Instead we have a list of empty buckets (no value) and we must initialize each bucket (position) in the array with a value. Let's take a look.

int[] lottoNumbers = new int[5];
The array lottoNumbers at any position has no value. Let's give it some values.
lottoNumbers[0] = 2; initialize position 0 to 2
lottoNumbers[1] = 4; initialize position 1 to 4
lottoNumbers[2] = 8; ...
lottoNumbers[3] = 16; ...
lottoNumbers[4] = 32; ...

Now that the array has been initialized, we can ask some questions about the array.
println( lottoNumbers[3] ); prints 16
Had the array not been initialized we may have printed 0 since java / processing takes care of this for us. In other programming languages, this would result in a major error and some pretty wacky numbers.

Arrays are collections of any type specified when the array is declared.
Every array must specify a size when declared.
Arrays are zero indexed, positions start at 0 and end at size - 1.
Arrays should always be initialized to a starting value before being used within a program.

Using Arrays

Arrays are pretty useful, even better once you have learn loops. The following examples in this module will involve some loops. It is safe to assume for these example that all loops are simply looping over each position in the array.