Two Dimensional Array in Java With Examples

Today we will look into Two-dimensional array in java. An array is like a container that can hold a certain number of values.

Two Dimensional Array in Java

Let’s look at a few examples of defining java two-dimensional array or 2d array.

Java Two Dimensional Array of primitive type

Java Two Dimensional Array of Objects

So we can define a two dimensional array of primitive data types as well as objects. If you look at the above examples, two dimensional array looks like a matrix, something like below image.


However, in Java, there is no concept of a two-dimensional array. A two-dimensional array in java is just an array of array. So below image correctly defines two-dimensional array structure in java.


Java multidimensional array example

Now if two-dimensional array in java is an array-of-arrays, then it should also support non-symmetric sizes as shown in below image.


Well, it’s absolutely fine in java. Below is an example program that depicts above multidimensional array.

If we run the above program, it will produce the following output.

That’s all about two-dimensional array in java. In a similar way, we can define a multidimensional array in java too.

Reference: Java Array Oracle page

By admin

Leave a Reply

%d bloggers like this: