by Dinesh Thakur Category: array

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]+" ");
                    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]+" ");
                 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;

Passing Two Dimensional Array to a Method in Java

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Search Content

Popular Article