Accessing elements in a multidimensional array in Java

When working with arrays in Java, it is not uncommon to come across situations where you need to work with multidimensional arrays. A multidimensional array is an array of arrays, where each element in the main array holds a reference to another array.

To access elements in a multidimensional array in Java, you’ll need to use nested loops to iterate through each dimension of the array. Let’s take a look at an example:

// Creating a multidimensional array
int[][] matrix = {
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}
};

// Accessing elements in the multidimensional array
for (int i = 0; i < matrix.length; i++) {
    for (int j = 0; j < matrix[i].length; j++) {
        System.out.print(matrix[i][j] + " ");
    }
    System.out.println();
}

In the above code snippet, we create a 2-dimensional array called matrix and initialize it with some values. We have three rows and three columns in this example.

To access the elements of the multidimensional array, we use nested for loops. The outer loop iterates over the rows, and the inner loop iterates over the columns. We use the syntax matrix[i][j] to access the element at row i and column j.

The output of the above code will be:

1 2 3 
4 5 6 
7 8 9 

You can also directly access and modify individual elements of a multidimensional array using indexing. For example:

// Accessing and modifying individual elements in the multidimensional array
int element = matrix[1][2]; // Accessing element at row 1, column 2
matrix[0][1] = 10; // Modifying element at row 0, column 1

System.out.println(element);
System.out.println(Arrays.deepToString(matrix));

In the above code, we access the element at row 1, column 2 using matrix[1][2] and store it in the variable element. We also modify the element at row 0, column 1 by assigning a new value to matrix[0][1]. The Arrays.deepToString() method is used to print the entire multidimensional array.

6
[[1, 10, 3], [4, 5, 6], [7, 8, 9]]

Remember that the indexing starts from 0, so the first element is at index 0, not 1.

Accessing elements in a multidimensional array allows you to work with complex data structures in Java. By using nested loops and the appropriate indexing, you can retrieve and modify the values stored in a multidimensional array efficiently.

#Java #MultidimensionalArray