Java Program to Read and Print a Two Dimensional Array

You are currently viewing Java Program to Read and Print a Two Dimensional Array

Java program to read and print a two dimensional array

import java.util.Scanner;

public class Ex2DArray {
  public static void main(String args[]) {
    // initialize here.
    int row, col, i, j;
    int arr[][] = new int[10][10];
    Scanner scan = new Scanner(System.in);

    // enter row and column for array.
    System.out.print("Enter row for the array (max 10) : ");
    row = scan.nextInt();
    System.out.print("Enter column for the array (max 10) : ");
    col = scan.nextInt();

    // enter array elements.
    System.out.println("Enter " + (row * col) + " Array Elements : ");
    for (i = 0; i < row; i++) {
      for (j = 0; j < col; j++) {
        arr[i][j] = scan.nextInt();
      }
    }

    // the 2D array is here.
    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();
    }
  }
}

Output

Enter row for the array (max 10) : 4
Enter column for the array (max 10) : 4
Enter 16 Array Elements : 
1
2
3
4
4
3
2
1
4
5
6
6
5
4
7
8
The Array is :
1  2  3  4  
4  3  2  1  
4  5  6  6  
5  4  7  8  

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply