Handling multidimensional arrays
A multidimensional array is an array of arrays. In Java, you can create arrays with two or more dimensions. The most common type of multidimensional array is the two-dimensional array, also known as a matrix or a table, where the elements are arranged in rows and columns.
Let’s see how to create multidimensional arrays.
Declaring and initializing multidimensional arrays
To declare a two-dimensional array, you need to specify the data type of the elements, followed by two sets of square brackets ([][]
) and the name of the array. Take the following example:
int[][] matrix;
Just like the one-dimensional array, we initialize a two-dimensional array with the use of the new
keyword, followed by the data type and the size of each dimension inside the square brackets, like this:
matrix = new int[3][4];
This code initializes a matrix of 3
rows and 4
columns. The type is int
, so we know that the values of the matrix are integers.