Multidimensional Arrays

Arrays we have mentioned till now are called one-dimensional arrays. However, we can declare multidimensional arrays in Java. A multidimensional array is an array of arrays. That is, each element of a multidimensional array is an array itself. For example,


\begin{lstlisting}
double[][] matrix = {{1.2, 4.3, 4.0},
{4.1, -1.1}
};
\end{lstlisting}

Here, we have created a multidimensional array named matrix. It is a 2-dimensional array. To learn more, visit the Java multidimensional array. A multidimensional array is an array of arrays. Each element of a multidimensional array is an array itself. For example,


\begin{lstlisting}
int[][] a = new int[3][4];
\end{lstlisting}

Here, we have created a multidimensional array named a. It is a 2-dimensional array, that can hold a maximum of 12 elements. Remember, Java uses zero-based indexing, that is, indexing of arrays in Java starts with 0 and not 1. Let's take another example of the multidimensional array. This time we will be creating a 3-dimensional array. For example,


\begin{lstlisting}
String[][][] data = new String[3][4][2];
\end{lstlisting}

Here, data is a 3d array that can hold a maximum of 24 (3*4*2) elements of type String.