pTutorial

New way to learn Programming

Java two dimensional array example

Java program to initialise and print the two dimension array by the user or user define print two dimensional array program.

User define two dimensional array example


import java.util.Scanner;

public class TwoDiementionalArrayExample{ 
    public static void main(String... z)
    {
       int a[][];
       int n;
        Scanner uk = new Scanner(System.in);
        System.out.println("Enter the number of element");
         n = uk.nextInt();
         a = new int[n][n];

    for(int i = 0; i < a.length; i++){
        for(int j = 0; j < a[i].length; j++){
           a[i][j]=uk.nextInt();
        }
    }
    	System.out.println("Output:");
        for(int i = 0; i < a.length; i++){
        for(int j = 0; j < a[i].length; j++){
            System.out.print(a[i][j]+"\t");
        }
        System.out.println("\t");
}
    }
}


Output

Enter the number of element
3
1 2 3
4 5 6
7 8 9 
Output:
1	2	3		
4	5	6		
7	8	9	

See Also


QUOTES OF THE DAY

A good beginning makes a good ending.

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER