Multidimensional Arrays in Java are used to store data in a tabular form, with each element identified by its unique position in a matrix of values. In simple terms, a two-dimensional array can be thought of as an array of one-dimensional arrays.
To define a multidimensional array in Java, you first specify the data type to be stored, followed by the size of each dimension of the array. For example, to create a two-dimensional integer array that can store 10 rows and 20 columns, you can use the following syntax:
javaint[][] twoD_arr = new int[10][20];
Similarly, to create a three-dimensional integer array that can store 10 rows, 20 columns, and 30 layers, you can use the following syntax:
javaint[][][] threeD_arr = new int[10][20][30];
The total number of elements that can be stored in a multidimensional array can be calculated by multiplying the size of all the dimensions.
To declare a two-dimensional array, you can either use the direct or indirect method of declaration. In the indirect method, you first declare the data type of the array and then specify its dimensions, as follows:
javadata_type[][] array_name = new data_type[x][y];
For example:
javaint[][] arr = new int[10][20];
In the direct method of declaration, you specify the values of the array elements directly, as follows:
javadata_type[][] array_name = {
{valueR1C1, valueR1C2, ....},
{valueR2C1, valueR2C2, ....}
};
For example:
javaint[][] arr = {{1, 2}, {3, 4}};
To access the elements of a two-dimensional array, you need to specify the row index and the column index of the element, as follows:
javax[row_index][column_index]
To print all the elements of a two-dimensional array, you can use nested for loops to traverse the rows and columns of the array.
Three-dimensional arrays are a more complex form of a multidimensional array, and they can be thought of as an array of two-dimensional arrays. The syntax for creating a three-dimensional array is similar to that of a two-dimensional array, except that you need to specify an additional dimension.