How to print and scan a 2 Dimensional Array?

Introduction:

It’s a great method to print your required values in the 2-dimensional array, like you can choose the size of the array and entered the integers of your choice.

Description Of Program:

First of all, make a class of your choice as I take EnteringArray as class in the given program, take scanner since we want to make our program user flexible(that here we are entering the numbers of our choice), moving forward we are running two loops here(i,j) since we are making 2-dimensional array and 2-D array is known as arrays of array, as we can see there are two methods being used.The other one is numbers, by introducing this method we don’t need to use any loop we can simply enter our required data into the 2-D array.

Hope it would be helpful.

package twodimensionarrays;

import java.util.Scanner;
public class EnteringArray {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.println(“Enter the rows and columns”);
int m=s.nextInt();
int n=s.nextInt();
int a[][]=new int[m][n];
for(int i=0;i<=a.length-1;i++)
{
for(int j=0;j<=a[i].length-1;j++)
a[i][j]=s.nextInt();
}
for(int i=0;i<=a.length-1;i++)
{
for(int j=0;j<=a[i].length-1;j++)
System.out.print(“\t”+a[i][j]);
System.out.println();
}
System.out.println(“\n”);

for(int[] numbers:a)
{
for(int number:numbers)
System.out.print(“\t”+number);
System.out.println();
}

}
}

Leave a Reply