Spread the love

public static void main(String[] args) {

Scanner x=new Scanner(System.in);
int rows,column;
System.out.println(“Enter the Rows of Array”);
rows=x.nextInt();
System.out.println(“Enter the Column of Array”);
column=x.nextInt();
int arr[][]=new int[rows][column];
int num=0;
for(int i=0;i<rows;i++)
{
for(int j=0;j<column;j++)
{
System.out.println(“Enter ” + num + ” Element “);
num++;
arr[i][j]=x.nextInt();
}
}
for(int i=0;i<rows;i++)
{
for(int j=0;j<column;j++)
{
System.out.print(arr[i][j]+”\t”);
}
System.out.println();
}
}

Output

Enter the Rows of Array
2
Enter the Column of Array
2
Enter 0 Element
1
Enter 1 Element
2
Enter 2 Element
3
Enter 3 Element
4
Final Array
1 2
3 4

1 thought on “Java Program to initialize 2d Array

Leave a Reply

Your email address will not be published. Required fields are marked *

Show Buttons
Hide Buttons