Multidimensional Arrays

Author: Tatyana Milkina

Multidimensional arrays are essentially arrays of arrays.

When declaring a multidimensional array variable, you use a separate pair of square brackets for each additional dimension. For example:

int[][] twoD = new int[5][4];

The following image shows a visual representation of a two-dimensional array 5 by 4. The left index represents the row, and the right index represents the column:

Java 2D array concept

The example below demonstrates how to set values in a 5x4 Java 2D array. The outer for loop iterates through rows, and the inner loop through columns. Each next element is assigned a value one greater than the previous:

public class TwoDArrayExample1 {
    public static void main(String[] args) {
        int[][] twoD = new int[5][4];
        int i, j, k = 0;
        for (i = 0; i < 5; i++) {
            for (j = 0; j < 4; j++) {
                twoD[i][j] = k++;
                System.out.print(twoD[i][j] + " ");
            }
            System.out.println();
        }
    }
}

Let’s now see how a two-dimensional array int[][] twoD = new int[3][4]; is represented in memory. The variable twoD doesn’t point to a matrix, but to a row (in red) of three elements, each referencing a row of four elements (in purple):

Java 2D array memory layout

The next image illustrates how a three-dimensional array int[][][] threeD = new int[3][4][2]; is stored in memory:

Java 3D array memory structure

This is how arrays of any dimension can be stored in memory in Java.

In the two-dimensional arrays we've examined so far, each row has the same number of elements — and that’s typical. But it’s not required: Java supports jagged arrays where each row can have a different number of elements. For example:

Jagged array in Java example

Here is the code to implement such a jagged array in Java. When declaring the two-dimensional array, only the number of rows is set — int[][] array = new int[4][];. This allocates space for the row references, but not for the actual rows. Then memory is allocated separately for each row:

public class TwoDArrayExample2 {
    public static void main(String[] args) {
        int[][] array = new int[4][];
        array[0] = new int[1];
        array[1] = new int[2];
        array[2] = new int[3];
        array[3] = new int[4];
        int i, j, k = 0;
        for (i = 0; i < 4; i++) {
            for (j = 0; j < i + 1; j++) {
                array[i][j] = k++;
                System.out.print(array[i][j] + " ");
            }
            System.out.println();
        }
    }
}

You can also use an initialization block if all element values are known in advance. Each row is placed within curly braces:

public class TwoDArrayExample3 {
    public static void main(String[] args) {
        double[][] arrayTwoD = {
                {0, 1, 2, 3},
                {4, 5, 6, 7},
                {8, 9, 10, 11},
                {12, 13, 14, 15}
        };
        for (double[] arrayOneD : arrayTwoD) {
            for (double element : arrayOneD) {
                System.out.print(element + " ");
            }
            System.out.println();
        }
    }
}
Курс 'Java для начинающих' на Udemy Курс 'Java для начинающих' на Udemy
Read also:
Comments