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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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;
    }
}
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

Karibasappa G C (KB)
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