CS 3723
 Programming Languages 
   Transpose in Java  


Transpose Function:

Here is a Transpose function presented in Java, along with test runs. These include the boundary cases: a matrix with 1 row, a matrix with 1 column, and a matrix with 1 row and 1 column.

Transpose in Java Output of a run
// Transpose: transpose arbitrary matrices
class Transpose {

   // 5 test matrices
   int[][] a = {{00, 01}, {10, 11}, {20, 21},
                {30, 31}};

   int[][] b = {{00, 01, 02, 03}, {10, 11, 12, 13}, 
                {20, 21, 22, 23}, {30, 31, 32, 33}};
                
   int[][] c = {{00, 01, 02, 03}}; 
                  
   int[][] d = {{00}, {10}, {20}, {30}}; 
                  
   int[][] e = {{00}};
   
   // transpose: obvious algorithm
   int[][] transpose(int[][] x) {
      int[][] r = new int[x[0].length][x.length];
      for (int i = 0; i < x.length; i++)
         for (int j = 0; j < x[i].length; j++)
            r[j][i] = x[i][j];
      return r;
   }
   
   // printArray: print row-by-row, numbering rows
   void printArray(int[][] x) {
      System.out.println("Array with " +
          x.length + " row" + 
         (x.length == 1 ? "" : "s") + " and " +
          x[0].length + " column" +
         (x[0].length == 1 ? "" : "s") + "\n");
      for (int i = 0; i < x.length; i++) {
         System.out.print("Row " + i + ": \t");
         for (int j = 0; j < x[i].length; j++) {
            if (x[i][j] < 10) System.out.print("0");
            System.out.print(x[i][j] + "\t");
         }
         System.out.println();
      }
      System.out.println();
   }
   
   void transposeAndPrint(int[][] a) {
      System.out.println("INITIAL ARRAY:");
      printArray(a);
      int[][] r = transpose(a);
      System.out.println("TRANSPOSED ARRAY:");
      printArray(r);
   }
   
   void testTranspose() {
      transposeAndPrint(a);
      transposeAndPrint(b);
      transposeAndPrint(c);
      transposeAndPrint(d);
      transposeAndPrint(e);
   }
      
   public static void main (String [] args) {
      Transpose t = new Transpose();
      t.testTranspose();
   }
}
% javac Transpose.java
% java Transpose
INITIAL ARRAY:
Array with 4 rows and 2 columns

Row 0:  00      01
Row 1:  10      11
Row 2:  20      21
Row 3:  30      31

TRANSPOSED ARRAY:
Array with 2 rows and 4 columns

Row 0:  00      10      20      30
Row 1:  01      11      21      31

INITIAL ARRAY:
Array with 4 rows and 4 columns

Row 0:  00      01      02      03
Row 1:  10      11      12      13
Row 2:  20      21      22      23
Row 3:  30      31      32      33

TRANSPOSED ARRAY:
Array with 4 rows and 4 columns

Row 0:  00      10      20      30
Row 1:  01      11      21      31
Row 2:  02      12      22      32
Row 3:  03      13      23      33

INITIAL ARRAY:
Array with 1 row and 4 columns

Row 0:  00      01      02      03

TRANSPOSED ARRAY:
Array with 4 rows and 1 column

Row 0:  00
Row 1:  01
Row 2:  02
Row 3:  03

INITIAL ARRAY:
Array with 4 rows and 1 column

Row 0:  00
Row 1:  10
Row 2:  20
Row 3:  30

TRANSPOSED ARRAY:
Array with 1 row and 4 columns

Row 0:  00      10      20      30

INITIAL ARRAY:
Array with 1 row and 1 column

Row 0:  00

TRANSPOSED ARRAY:
Array with 1 row and 1 column

Row 0:  00


Revision date: 2014-04-06. (Please use ISO 8601, the International Standard Date and Time Notation.)