News Ticker

# Multi-dimensional Arrays

## Introduction

A multidimentional array is an array that contains a number of other arrays of the same type. They are declared, constructed and initialised in a simular way to one-dimensional arrays. In the first bracket we specify the number of arrays we are going to create in our array.

## Array Declaration

To declare a three dimesional array we specifiy the size of the array as follows.

[sourcecode language=”java” toolbar=”false”]
int [][] scores= new int [3][];
[/sourcecode]
This array can contain up to three int arrays. The three arrays are defined by assigning a new array to each element.

[sourcecode language=”java” toolbar=”false”]
scores[0] = new int[4]; // An int array of 4 elements
// is allocated to the first element
scores[1] = new int[6]; // An int array of 6 elements
// is allocated to the second element
scores[2] = new int[1]; // An int array of 1 elements
// is allocated to the third element
[/sourcecode]

## Array Initialisation

Initializing an array gives its elements a value other than the default value. Array elements are initialised by assigning a compatible value.

The contents of each array are allocated as follows:

[sourcecode language=”java” toolbar=”false”]
scores[0][0] = 6; // Allocates 6 to the first element in the first array.
scores[0][1] = 1; // Allocates 1 to the second element in the first array.
scores[1][0] = 3; // Allocates 3 to the first element in the second array.
scores[2][0] = 2; // Allocates 2 to the first element in the third array.
[/sourcecode]
The first square brackets refers to the array, while the second square brackets refers to the element in the array refered to by the first square brackets.

## Shortcut Declaration, Construction and Initialization

An array can be declared, constructed and initialised all at once.

[sourcecode language=”java” toolbar=”false”]
int[][] scores = {{5,2,4,7}, {9,2}, {3,4}};
[/sourcecode]
this is the same as doing:

[sourcecode language=”java” toolbar=”false”]
int[][] scores = new int[3][];
scores[0][] = {5,2,4,7};
scores[1][] = {9,2};
scores[2][] = {3,4};
[/sourcecode]
A two-dimensional array is created because we have int[][] scores on the left-hand side. The contents inside {} creates and allocates arrays. In this case three.

The size of the array is determined by the number of comma-separated items.

## Notes

The dimensions must be declared from left to right: int[ ] a[ ] = new int[4][ ] ; and int[ ][ ] a = new int[5][4] ; However this is wrong: int a[ ][ ] = new int [ ][4] ;

The first element in the array is: i[0], this refers to the first array reference, the second element is: i[1], this refers to the second array reference. Therefore it is possible to do the following assignments:

[sourcecode language=”java” toolbar=”false”]
int [ ] y = i [ 0 ];
int [ ] x = i [ 1 ];
[/sourcecode]
It is not necessary to put the extra [] because we only want to refer to the array reference not the contents of these arrays.

In the array declaration int i[][] = new int[4][4]; there are two arrays and each can hold 5 elements. The first array is at i[0] and the second array is at i[1].

In an array that looks like this:

```   0    1
0 [x] [ ]
1 [ ] [ ]
2 [ ] [ ]
3 [ ] [y]
4 [ ] [ ]```

To refer to x we would write: i[0][0] to refer to y: i[1][3]. The first number is the index of the array while the second number is the index of the element: i[index of array][index of element].

If you liked this tutorial about multi-dimensional arrays you might like my tutorial on one-dimensional arrays.