Creating Arrays

There are three steps to creating an array, declaring it, allocating it and initializing it.

Declaring Arrays
Like other variables in Java, an array must have a specific type like byte, int, String or double. Only variables of the appropriate type can be stored in an array. You cannot have an array that will store both ints and Strings, for instance.

Like all other variables in Java an array must be declared. When you declare an array variable you suffix the type with [] to indicate that this variable is an array. Here are some examples:

int[] k;
float[] yt;
String[] names;

In other words you declare an array like you’d declare any other variable except you append brackets to the end of the variable type.

Allocating Arrays
Declaring an array merely says what it is. It does not create the array. To actually create the array (or any other object) use the new operator. When we create an array we need to tell the compiler how many elements will be stored in it. Here’s how we’d create the variables declared above:

k = new int[3];
yt = new float[7];
names = new String[50];

The numbers in the brackets specify the dimension of the array; i.e. how many slots it has to hold values. With the dimensions above k can hold three ints, yt can hold seven floats and names can hold fifty Strings. Therefore this step is sometimes called dimensioning the array. More commonly this is called allocating the array since this step actually sets aside the memory in RAM that the array requires.

This is also our first look at the new operator. new is a reserved word in java that is used to allocate not just an array, but also all kinds of objects. Java arrays are full-fledged objects with all that implies. For now the main thing it implies is that we have to allocate them with new.

Initializing Arrays
Individual elements of the array are referenced by the array name and by an integer which represents their position in the array. The numbers we use to identify them are called subscripts or indexes into the array. Subscripts are consecutive integers beginning with 0. Thus the array k above has elements k[0], k[1], and k[2]. Since we started counting at zero there is no k[3], and trying to access it will generate an ArrayIndexOutOfBoundsException.

You can use array elements wherever you’d use a similarly typed variable that wasn’t part of an array.

Here’s how we’d store values in the arrays we’ve been working with:

k[0] = 2;
k[1] = 5;
k[2] = -2;
yt[6] = 7.5f;
names[4] = “Fred”;

This step is called initializing the array or, more precisely, initializing the elements of the array. Sometimes the phrase “initializing the array” would be reserved for when we initialize all the elements of the array.

For even medium sized arrays, it’s unwieldy to specify each element individually. It is often helpful to use for loops to initialize the array. For instance here is a loop that fills an array with the squares of the numbers from 0 to 100.

float[] squares = new float[101];

for (int i=0; i <= 100; i++) {
squares[i] = i*i;
}

Two things you should note about this code fragment:

* Watch the fenceposts! Since array subscripts begin at zero we need 101 elements if we want to include the square of 100.
* Although i is an int it becomes a float when it is stored in squares, since we’ve declared squares to be an array of floats.

One way to avoid fencepost errors is to use the array’s built-in length member. This tells you the number of components in the array. In the example above, squares.length equals 101. Thus the loop could have been written like this:

float[] squares = new float[101];

for (int i=0, i < squares.length; i++) {
squares[i] = i*i;
}

Note that the <= changed to a < to make this work.
Shortcuts

It may seem to be a lot of work to set up arrays, particularly if you’re used to a more array friendly language like Fortran. Fortunately Java has several shorthands for declaring, dimensioning and strong values in arrays.

We can declare and allocate an array at the same time like this:

int[] k = new int[3];
float[] yt = new float[7];
String[] names = new String[50];

We can even declare, allocate, and initialize an array at the same time providing a list of the initial values inside brackets like so:

int[] k = {1, 2, 3};
float[] yt = {0.0f, 1.2f, 3.4f, -9.87f, 65.4f, 0.0f, 567.9f};

Comments are closed.