Transpose of a 2D Matrix using list of list in java – program with explanation

  1. package com.kb.transpose;
  2. import java.util.*;
  3. public class TransposeWithListOfList {
  4.     public static void main(String[] args) {
  5.        
  6.         //create 2d array to hold matrix kind of data
  7.        
  8.         Object[][] data = {
  9.             { "r1col1", "r1col2", "r1col3", "r1col4" },
  10.             { "r2col1", "r2col2", "r2col3", "r2col4" },
  11.             { "r3col1", "r3col2", "r3col3", "r3col4" },
  12.         };
  13.        
  14.        
  15.         List<List<Object>> table = new ArrayList<List<Object>>();//list to hold 2d matrix
  16.        
  17.         for (Object[] row : data) {//iterate each row of 2d array
  18.             table.add(Arrays.asList(row));//convert 1d array into list and then add it to list of list
  19.         }
  20.         System.out.println(table); //  [[r1col1, r1col2, r1col3, r1col4],
  21.                                    //   [r2col1, r2col2, r2col3, r2col4],
  22.                                    //   [r3col1, r3col2, r3col3, r3col4]]"
  23.        
  24.         table = returnTranspose(table);//get the transpose of a matrix
  25.        
  26.         System.out.println(table); //  [[r1col1,r2col1, r3col1],
  27.                                    //   [r1col2, r2col2, r3col2],
  28.                                    //   [r1col3, r2col3, r3col3],
  29.                                    //   [r1col4, r2col4, r3col4]]
  30.     }
  31.     static  List<List<Object>> returnTranspose(List<List<Object>> table) {
  32.        
  33.         List<List<Object>> transposedList = new ArrayList<List<Object>>();//list of list to hold transpose
  34.        
  35.         final int firstListSize = table.get(0).size();
  36.         for (int i = 0; i < firstListSize; i++) {
  37.             List<Object> tempList = new ArrayList<Object>();//Temp list to hold each transposed row which was column initially
  38.            
  39.             for (List<Object> row : table) { // iterate outer list to get sublist each time in the iteration
  40.                 tempList.add(row.get(i));//take sublist element at ith position each time and add it to temp list
  41.             }
  42.            
  43.             //here col list have taken one one element from each element :
  44.             //ex: during first iteration of outer loop, it takes 1st element from each sublist
  45.             //during second iteration of outer loop, it takes 2nd element from each sublist
  46.             transposedList.add(tempList);//each transposed list is added to transposedList
  47.         }
  48.         return transposedList;
  49.     }
  50. }
package com.kb.transpose;
import java.util.*;
public class TransposeWithListOfList {
    public static void main(String[] args) {
    	
    	//create 2d array to hold matrix kind of data
    	
        Object[][] data = {
            { "r1col1", "r1col2", "r1col3", "r1col4" },
            { "r2col1", "r2col2", "r2col3", "r2col4" },
            { "r3col1", "r3col2", "r3col3", "r3col4" },
        };
        
        
        List<List<Object>> table = new ArrayList<List<Object>>();//list to hold 2d matrix
        
        for (Object[] row : data) {//iterate each row of 2d array
            table.add(Arrays.asList(row));//convert 1d array into list and then add it to list of list
        }
        System.out.println(table); //  [[r1col1, r1col2, r1col3, r1col4],
                                   //   [r2col1, r2col2, r2col3, r2col4],
                                   //   [r3col1, r3col2, r3col3, r3col4]]"
        
        table = returnTranspose(table);//get the transpose of a matrix
        
        System.out.println(table); //  [[r1col1,r2col1, r3col1],
                                   //   [r1col2, r2col2, r3col2],
                                   //   [r1col3, r2col3, r3col3],
                                   //   [r1col4, r2col4, r3col4]]
    }
    static  List<List<Object>> returnTranspose(List<List<Object>> table) {
    	
        List<List<Object>> transposedList = new ArrayList<List<Object>>();//list of list to hold transpose
        
        final int firstListSize = table.get(0).size();
        for (int i = 0; i < firstListSize; i++) {
            List<Object> tempList = new ArrayList<Object>();//Temp list to hold each transposed row which was column initially
            
            for (List<Object> row : table) { // iterate outer list to get sublist each time in the iteration
            	tempList.add(row.get(i));//take sublist element at ith position each time and add it to temp list
            }
            
            //here col list have taken one one element from each element :
            //ex: during first iteration of outer loop, it takes 1st element from each sublist
            //during second iteration of outer loop, it takes 2nd element from each sublist
            transposedList.add(tempList);//each transposed list is added to transposedList
        }
        return transposedList;
    }
}

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on