Multidimensional Arrays in Memory

Contact Us or call 1-877-932-8228
Multidimensional Arrays in Memory

Multidimensional Arrays in Memory

Recall that a matched pair of brackets after a data type means:

  • The array contains elements of that type.
  • The array itself is a type of data.
  • So a two-dimensional array written as this:
int[][] nums;

...could be thought of as an array of integer arrays, as if it were written as (note that this is not legal syntax):

(int[])[] nums;

In Java, a two-dimensional array is actually a single array of array reference variables, each of which points to a single dimensional array.

To extend the example above:

int[][] nums = new int[3][6];

This is an array of 3 elements, each of which is an array of 6 int elements as shown in the diagram below:

An two-dimensional array in memory

Note that it is possible to replace any of the one-dimensional elements with a different one, or that the second-dimension arrays each have a different length - the following line would replace one of the arrays with another of a different length

nums[1] = new int[4];

An jagged two-dimensional array in memory

Next