codescracker


java

Java Two Dimensional Array Program



« Previous Program Next Program »


Two Dimensional Array Program

Two dimensional array can be made in Java Programming language by using the two loops, the first one is outer loop and the second one is inner loop. Outer loop is responsible for rows and the inner loop is responsible for columns. And both rows and columns combine to make two-dimensional (2D) Arrays.

Java Programming Code for Two Dimensional (2D) Array

Following Java Program ask to the user to enter row and column size of the array then ask to the user to enter the array elements, and the program will display the two dimensional array:

/* Java Program Example - Two Dimensional Array */
		
import java.util.Scanner;

public class JavaProgram
{
   public static void main(String args[])
   {
       int row, col, i, j;
       int arr[][] = new int[10][10];
       Scanner scan = new Scanner(System.in);
	   
       System.out.print("Enter Number of Row for Array (max 10) : ");
       row = scan.nextInt();
       System.out.print("Enter Number of Column for Array (max 10) : ");
       col = scan.nextInt();
	   
       System.out.print("Enter " +(row*col)+ " Array Elements : ");
       for(i=0; i<row; i++)
       {
           for(j=0; j<col; j++)
           {
               arr[i][j] = scan.nextInt();
           }
       }
	   
       System.out.print("The Array is :\n");
       for(i=0; i<row; i++)
       {
           for(j=0; j<col; j++)
           {
               System.out.print(arr[i][j]+ "  ");
           }
           System.out.println();
       }
   }
}

When the above Java Program is compile and executed, it will produce the following output:

two dimensional array program in java

Same Program in Other Languages

You may also like to learn and practice the same program in other popular programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test