Arrays

Arrays are a linked set of variables or objects. They allow us to store a connected set of variables of the same type and can be described a list. The advantage of arrays is that we can treat our “list” as one item.

Like variables, arrays are created by stating the type of the variable being organised into the array and the name of the array. The difference is in the addition of the square brackets ( [] ) that we place after the type.

The following examples display arrays of different variable types:

int[] listOfNumbers;
String[] shoppingList;

The arrays above are only partly complete because arrays also have to either be given their elements directly or they must be told how many items they will store.

Our first example shows how to create an array with a number of items as initial values.

String[] shoppingList = {"Milk", "Eggs", "Bread", "Coke", "Chocolate"};

This will create an array with 5 elements in it.

To create an array which sets aside space for values to hold later on we do the following:

int[] listOfNumbers = new int[10];

This will create integer array called listOfNumbers with space to hold 10 integer numbers.

Using Arrays

Arrays can be used in programs as you would use a normal variable except that you need to say which element of the array you would like to use. This is done using square brackets ( [] ) after the array name.
If I wanted to put the number 3 into the second element of the integer array created above I would do so as follows:

listOfNumbers[1] = 3;

You will hopefully notice that we have used the number 1 as the array element instead of 2. This is because all arays start at 0 and not 1. Therefore our list will will have an index of 0 to 9 (10 values in total).

If you use an index value that is our of range of the array you get an error message that says “ArrayIndexOutOfBoundsException”. Often beginner programmers make the mistake of using the size of an array as an index value. i.e. listOfNumbers[10] this will not work because there are only items from 0 to 9 and it will produce an error.

Multidimensional Arrays

Arrays are not only limited to a single linear structures. They can be expanded to contain arrays within arrays. If, for example, we wanted to store x and y co-ordinates in a grid type structure we could do so with a 2 dimensional array. It may help to think of a two dimensional array in terms of columns and rows, similar to a spreadsheet with each cell have a row and column reference (x and y co-ordinate if you like). These are specified in the square brackets ( [] ) after the array name.

We declare a 2D array as follows:

int lisOfNumbers[] [] = new int[5] [5];

Which produces the effect of a grid as shown below:

listOfNumbers[0,0] listOfNumbers[0,1] listOfNumbers[0,2] listOfNumbers[0,3] listOfNumbers[0,4]
listOfNumbers[1,0] listOfNumbers[1,1] listOfNumbers[1,2] listOfNumbers[1,3] listOfNumbers[1,4]
listOfNumbers[2,0] listOfNumbers[2,1] listOfNumbers[2,2] listOfNumbers[2,3] listOfNumbers[2,4]
listOfNumbers[3,0] listOfNumbers[3,1] listOfNumbers[3,2] listOfNumbers[3,3] listOfNumbers[3,4]
listOfNumbers[4,0] listOfNumbers[4,1] listOfNumbers[4,2] listOfNumbers[4,3] listOfNumbers[4,4]

We can traverse our 2D array by using a for loop as follows:

for (int x = 0 ; x < grid.length ; x++)
{
   for (int y = 0 ; y < grid [x].length ; y++)
   {
     grid [x] [y] = "grid[" + x + "," + y + "]";
     System.out.print(grid[x] [y]);
   }
   System.out.println();
}

The diagram below illustrates the array declared above and shows how the array contains a list of arrays.