Last Updated:

Two-dimensional arrays in Java

An array is a container object that can contain a certain number of objects of the same type. Objects can be primitive types: int, float, long or they can be objects: String, Integer, etc. The elements of the array are accessed using indexes.

Two-dimensional array

 

A two-dimensional array is also known as an "array of arrays". For example, String [4] [5] is an example of an array of arrays with 4 rows and 5 columns. When we call the array length function, it returns the number of rows in the array.

When we initialize the array, we need to specify its size. Let's see an example of a two-dimensional array in a simple Java program. When we initialize multidimensional arrays, we don't need to specify the size for all sizes, only the "left dimension" (strings).

TwoDimArray.java

The result of the program will be as follows:

Note that when we use the syntax for quickly creating a two-dimensional array, we can end up with a different number of values in each array.