So far we have had examples of array which are sets of variables. In these examples there has been one index for each element. If we would represent these arrays graphically they would be could be drawn as a row of numbers in boxes, like this:


A two dimensional array is created in the following way:
int myArray [][] = new int [10][3];
This will create an array of size 10 by 3 int's  a total of
30 int's will be created. When arrays are created,
their elements are assign initial values of 0.
The above array could be drawn like this:


We can access any element of this array by using 2 indexes. For example, if we wanted to assign the value 18 to the element at position 4 down and 2 across we would write something like this (remember, indexes start with 0 so the 4th row is index number 3):
myArray [3][1] = 18;
This would change the array in the following way:


Essentially, we now have a set of 30 integers which we can access (change or printout etc.) by using two sets of indices. The name of the array ( myArray ) refers to this whole set of integers.
We can also make arrays of 3 or 4 dimensions though these are impossible to draw in two dimensions, and they are bit harder to imagine in your mind. To think of a 3 dimensional array imagine a 2 dimensional array and then think of each cell in that array itself containing many elements. A three dimensional array is created in the following way:
int myArray [][][] = new int [10][10][3];
This will creat an array named myArray containing 300 integers. We can access any element of this array by using 3 indices. For example, if we wanted to assign the value 312 to the element at position 3 down and 7 across and 2 in, we would write something like this
myArray [2][6][1] = 312;
Once you grasp this you might be then able to think one more step. Imagine each element of your 3 dimensional array as containing not just one element, but a whole set of elements. It is quite hard to imagine anything more than 3 dimensions, but theoretically, you can have 5, 6, 7, etc. dimension arrays. In practice you should never use more than a 3 dimensional array for three reasons: (1) They will use up a lot of memory even with relatively small indexed ( an even sided 4 dimension array of size 100 integers will have 100,000,000 integers in it, or 400 mb of data ) (2) You will make errors (3) You will confuse anyone trying to read your code. Even using 3 dimensional arrays is not really advisable.
© Nachum Danzig November 2003