passing 2d Array
Write a program to pass and return a 2D array.
import java.util.*;
import java.util.*;
class P192DArrayPassing
{
public static void main()
{
int [][] a =getArray();
printArray(a);
}
static void printArray(int a[][])
{
int row = a.length;
for(int i=0;i<row;i++)
{
for(int j=0;j<a[i].length ; j++)
System.out.print("\t"+a[i][j]);
System.out.println();
}
System.out.println(Arrays.toString(a)); // Print array of addresses of array
System.out.println(Arrays.deepToString(a));// Print 2D array
}
static int[][] getArray()
{
Scanner in = new Scanner(System.in);
System.out.println(" \n Enter Columns and rows of 2D array respectively \n");
int column=in.nextInt();//c column
int row=in.nextInt();//r rows
int [][] x = new int[row][column];
for(int i=0;i<row;i++)
{
for(int j=0;j<column;j++)
{
System.out.print(" Column "+(j+1) + " Rows " + (i+1) +"\t");
x[i][j]=in.nextInt(); // store in value input by user in 2D array
System.out.println();
}
}
return x;
}
}
Comments
Post a Comment