Multi-Dimensional Arrays

Contact Us or call 1-877-932-8228
Multi-Dimensional Arrays

Multi-Dimensional Arrays

Arrays may have more than one dimension, for such things as:

  • A graphic image that is x pixels across and y pixels vertically.
  • Weather information modeled in a 3-dimensional space, with measurements for these axes: North/South, East/West, and altitude.

Declare a multidimensional array as:

datatype[][] ... [] arrayName;

Arrays are objects, and, like other objects, declaring a variable does not instantiate the array - that must be done separately. To instantiate a multidimensional array:

arrayName = new datatype[size1][size2] ... [sizeN];

The most significant dimension is listed first; the least significant dimension listed last.

The code below could be used to declare an array to store an image that is 640 pixels across and 480 pixels down - in a graphic the image data is stored sequentially across each row; each row is a 640 pixel block; there are 480 of these blocks in our image:

int[][] picture = new int[480][640];

This code might be used for an image where the data is stored in three layers, each of which is an entire 480 by 640 array:

int[][][] picture = new int[3][480][640];