1. Introduction

In Java, multi-dimensional arrays are essentially arrays of arrays, with each element of the array holding another array. These arrays are helpful in organizing data in a matrix format, making it suitable for mathematical operations, image processing, etc. This post provides an example of how to implement and manipulate multi-dimensional arrays in Java.

2. Program Steps

1. Declare and initialize a two-dimensional array.

2. Iterate through the array using nested loops.

3. Access and print each element of the multi-dimensional array.

3. Code Program

public class MultiDimensionalArray {

    public static void main(String[] args) {

        // Step 1: Declare and initialize a two-dimensional array
        int[][] twoDArray = {
            {1, 2, 3},
            {4, 5, 6},
            {7, 8, 9}
        };

        // Step 2: Iterate through the array using nested loops
        System.out.println("Elements of the two-dimensional array are: ");
        for (int i = 0; i < twoDArray.length; i++) {
            for (int j = 0; j < twoDArray[i].length; j++) {

                // Step 3: Access and print each element of the multi-dimensional array
                System.out.print(twoDArray[i][j] + " ");
            }
            System.out.println();
        }
    }
}

Output:

Elements of the two-dimensional array are:
1 2 3
4 5 6
7 8 9

4. Step By Step Explanation

Step 1: We start by declaring and initializing a two-dimensional array twoDArray. The array is filled with three sub-arrays, each containing three elements.

Step 2: To access the elements of twoDArray, we use nested for loops. The outer loop iterates over the sub-arrays, while the inner loop iterates over the elements of each sub-array.

Step 3: Inside the inner loop, we access and print each element of the multi-dimensional array. After printing all the elements of a sub-array, we move to the next line to ensure proper formatting of the output.