## Introduction to Computer Science - Java

### Multi-dimensional Arrays

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:
index
 0 1 2 3 4 5 6 7 8 9
values
 20 17 2 34 61 2 75 93 32 8

Such and array is called one dimensional becasue it can be drawn in a linear or one-dimensional fashion. There also exists the possibility of making 2 or more dimensional arrays. These are also called multi-indexed or multi-subscripted arrays.

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:
index
 0 1 2 3 4 5 6 7 8 9
012
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

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:
index
 0 1 2 3 4 5 6 7 8 9
012
 0 0 0 0 0 0 0 0 0 0 18 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

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.