Just like one-dimensional arrays, a two-dimensional array can also be passed to a method and it can also be returned from the method. The syntax is similar to one-dimensional arrays with an exception that an additional pair of square brackets is used.
Now let us consider a program to find the transpose of a matrix.
public class Transpose
{
public static void main(String[] args)
{
int[] [] table= {{5,6,7},{3,4,2}};
int[] [] result;
System.out.println("Matrix Before Transpose : ");
for(int i=0;i<table.length;i++)
{
for(int j=0;j<table[i].length;j++)
System.out.print(table[i][j]+" ");
}
result=transpose(table);
System.out.println("\nMatrix After Transpose :");
for(int i=0;i<result.length;i++)
{
for(int j=0;j<result[i].length;j++)
System.out.print(result[i][j]+" ");
System.out.println();
}
}
static int[][] transpose(int[][] a)
{
int[][] temp=new int[a[0].length][a.length];
for(int i=0;i<a[0].length;i++)
{
for(int j=0;j<a.length;j++)
temp[i][j]= a[j][i];
}
return temp;
}
}