It is the simplest form of a multidimensional array in java.
In such case, data is stored in row and column based index (also known as matrix form).

  • 2D Array Declaration in java


    Syntax :

    
    DataType  array_name[ ][ ] ;
    DateType  [ ][ ]array_name ;
    DataType[ ][ ]  array_name ;
    DataType[ ]  array_name[ ] ;
    DataType[ ]  [ ]array_name;
    DataType   [ ]array_name[ ];

  • Example to instantiate (Create) Multidimensional Array in java

     int[][] arr=new int[3][3] ; //3 row and 3 column 
    Let us see how to declare and initialize elements of 2D array :


    Example of declaring , instantiating 2D array .

    
    //example of 2D array
    class Example {
    
        public static void main(String[] args) {
            //Declar 2D array
            int[][] a = new int[2][3];//row=2 , col=3
            //now initialize the elements
            a[0][0] = 5;
            a[0][1] = 45;
            a[0][2] = 44;
            a[1][0] = 3;
            a[1][1] = 12;
            a[1][2] = 55;
    
            //now traverse the elements with nested for loop
            for (int i = 0; i < 2; i++) {
                for (int j = 0; j < 3; j++) {
                    System.out.print(a[i][j] + "\t");
                }
                System.out.println();//change the line after the one row
            }
    
            // point : first loop is for rows
            // point : second loop for cols
        }
    }

    Output:
    5	45	44	
    3	12	55	

    Example of declaring , instantiating and Initializing Array in a single line.

    
    //example of 2D array
    class Example {
    
        public static void main(String[] args) {
            //Declaring , instantiating and initializing the elements in single line.
            int a[][] = {
                {22, 33, 22},
                {11, 12, 32}
            };
    
            //now traverse the elements with nested for loop
            for (int i = 0; i < 2; i++) {
                for (int j = 0; j < 3; j++) {
                    System.out.print(a[i][j] + "\t");
                }
                System.out.println();//change the line after the one row
            }
        }
    }
    

    Output:
    22	33	22	
    11	12	32	

    value of Rows and Cols is not required in this type of Declaration and initialization.

  • How to take input from user for 2D array ?

    Example of taking input from user using java.util.Scanner class.

    Example)
    drag_handle
     
    //example of 2D array
    import java.util.Scanner;
    class Example {
    
        public static void main(String[] args) {
            int a[][] = new int[3][3];
            Scanner sc = new Scanner(System.in);
    
            //use nested loop for going to each index of 2D array
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                    a[i][j] = sc.nextInt();
                }
            }
            System.out.println("Going  to print the array :");
            //printing the elements of 2D array
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                    System.out.print(a[i][j] + "\t");
                }
                System.out.println();//change the line after the one row
            }
    
        }
    }
    
    Output:
    1 2 3
    3 4 5
    6 7 4
    Going  to print the array :
    1	2	3	
    3	4	5	
    6	7	4	

Program for Adding 2 matrix in java.
drag_handle

//addition of two matrix
import java.util.Scanner;

class Example {

    public static void main(String[] args) {
        int a[][] = new int[3][3];//1st matrix
        int b[][] = new int[3][3];//2nd matrix
        int c[][] = new int[3][3];//for storing sum
        Scanner sc = new Scanner(System.in);

        System.out.println("Enter the elements of first matrix(3X3) :");
        //use nested loop for going to each index of 2D array
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                a[i][j] = sc.nextInt();
            }
        }

        System.out.println("Enter the elements of 2nd  matrix(3X3) :");
        //use nested loop for going to each index of 2D array
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                b[i][j] = sc.nextInt();
            }
        }

        //now sum the elements of a[][] and b[][]  and put in c[][]  
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                c[i][j] = a[i][j] + b[i][j];
            }
        }
        System.out.println("Going  to print third array/Resultant array:");
        //printing the elements of 2D array
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                System.out.print(c[i][j] + "\t");
            }
            System.out.println();//change the line after the one row
        }

    }
}

Output:

Enter the elements of first matrix(3X3) :
1 2 3
3 2 1
4 5 3
Enter the elements of 2nd  matrix(3X3) :
1 2 3
4 5 6
8 2 1
Going  to print third array/Resultant array:
2	4	6	
7	7	7	
12	7	4	


Program for transpose of a matrix.
drag_handle

import java.util.Scanner;
//example of transposing the matrix
class Example {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);//for input
        System.out.println("Enter number of row :");
        int r = sc.nextInt();
        System.out.println("Enter number of cols :");
        int c = sc.nextInt();

        //now declare and create of rXc
        int a[][] = new int[r][c];
        System.out.println("Enter the elements of array :");
        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                a[i][j] = sc.nextInt();
            }
        }

        int trans[][] = new int[c][r];
        //now transposing the array 
        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                trans[j][i] = a[i][j];
            }           
        }
        System.out.println("Going  to print Resultant array:");
        //printing the elements of 2D array
        for (int i = 0; i < c; i++) {
            for (int j = 0; j < r; j++) {
                System.out.print(trans[i][j] + "\t");
            }
            System.out.println();//change the line after the one row
        }

    }
}

Output:

Enter number of row :
3
Enter number of cols :
2
Enter the elements of array :
12 32
22 11
33 77
Going  to print Resultant array:
12	22	33	
32	11	77	

What next?

The next topic is Array important points in java

Share this page