An array of more than one dimension is known as multi-dimensional array. Two of the most common examples of multi-dimensional arrays are two and three dimensional array, known as 2D and 3D array, anything above is rare. I have never seen 4 dimensional arrays, even
3D arrays are not that common. Now question comes, when do use multi-dimensional array? Any real life example? Well,
2D arrays are very common on platform games like Super Mario Bros to represent screen or terrain;
2D arrays can also be used to represent structures like spreadsheet, or to draw board games like Chess, which requires
8x8 board, Checkers and
Tic-Tac-Toe, which requires 3 rows and 3 columns. Another popular application of multi-dimensional arrays are in matrix manipulation. For example to represent a
3x3 matrix you need a
two dimensional array of 3 one dimensional array each containing 3 elements. Similarly to represent
3x2 matrices you need 2 two dimensional array of one dimensional array of length 3. In other words, each row in two dimensional array is a one dimensional array.
Java truly doesn't support multi-dimensional array but allows you to create and use array of any number of dimensional. Two dimensional array is actually an array of one dimensional array. This is unlike languages like
C or
FORTRAN, which allows Java array to have rows of varying length i.e. a multidimensional array can have 2 columns in one row and 3 columns in second. Similar to one dimensional array, length of two dimensional array is also fixed.
You can not change length of any array, i.e. number of rows and columns will remain fixed. A
2x2 array can hold total 4 elements and they can be accessed using row and column index e.g.
a[0][0] will give you elements in first row and first column, similarly
a[1][1] will give you elements from
2nd row and
2nd column. Just like normal array, index start at
0 and finishes at length -1.