by Dinesh Thakur Category: array

One-dimensional array that uses a single index. Just like other languages like C/C++ one may think that Java might also have multidimensional arrays too, which are arrays that contain more than one index and can store information in multiple dimensions. But Java does not truly support multidimensional arrays. However, one can achieve the same functionality by declaring an arrays of arrays.

In order to understand arrays of arrays, consider an example where we want to calculate the marks obtained by 4 students of a class in 3 different subjects. An obvious way to store information is in a two-dimensional array (or matrix) which we call marks. In order to create this two dimensional array marks, we follow the same steps as discussed with one-dimensional arrays i.e. first declare an array and then instantiate it.

The two-dimensional array marks of int type can be declared as follows,

int [] [] marks;

The above declaration can also be written as,

int[] marks[];

int marks [] [] ;

Each of the pairs of square brackets represent one dimension. This statement creates a variable marks that will refer to a two- dimensional int array.

Once you have declared an array variable, you can create an array that it will reference using the following statement,

marks = new int [4] [3] ;

On execution of this statement, the JVM creates an array with 4 elements and stores a reference to this array in the variable marks. Each of these four elements is actually a reference variable referring to a (newly created) int array with 3 elements. In the two-dimensional array marks, the first index corresponds to a student and second index corresponds to a subject.

                   Multidimensional Array in Java Example

You can combine the declaration and instantiation of the above array in a single statement as follows,

int [][] marks = new int[4][3];

Once this two-dimensional array has been created, an individual element is accessed by indexing the array. Separate index must be provided for each dimension, each of which begins at 0. So in order access marks obtained by 2nd student in 3rd subject can be written as

marks[1][2] // remember arrays are 0 based

As we can calculate the length of one dimensional array using the length field. Similarly, in multidimensional arrays, we can calculate the length of each dimension.  In our example, the length of the first dimension can be calculated by using the following,


The length of the second dimension can be calculated using marks[i].length where i

ranges from 0<=i<=3.

The complete program is as follows,

import java.util.Scanner;

public class twoDArray
     public static void main(String[] args)
            int[] [] a = new int[3] [2];
            int i, j;
            Scanner input=new Scanner(;
            System.out.print("Enter the Elements of 3 * 2 Matrix :");
            for(i=0;i<3; i++)
                   for(j=0; j<2;j++)
                   a[i] [j]=input.nextInt();
            System.out.println("Elements in Matrix from are :");
                   for(j=0; j<2; j++)
                       System.out.print(a[i][j]+" ");

Multidimensional Array in Java

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Related Articles