import java.util.*;

public class Main 
{
    static List<List<Integer>> finalMatrix = new ArrayList<List<Integer>>();
    
    public static void main (String[] args)
    {
        Integer [][]test = new Integer[][] {{1,2,3},{4,5,6},{7,8,9}};
        //Integer [][]test = new Integer[][] {{10,20},{30,40}};
        //Integer [][]test = new Integer[][] {{11},{22},{33}};
        //Integer [][]test = new Integer[][] {{1,2},{3,4}};
        
        
        //In order to populate using Arrays.asList, we need to process each row of the test case
        //I have used the following technique to simplify process of creating test cases.
        //I have also identified why my struggles persisted previously in attempting to replicate the merged
        //intervals.. This provides much more visibility of how data was required to be structured
        //AND PERHAPS it is the only technique available to populate the data
        
        List<List<Integer>> matrix = new ArrayList<>();
        
        for (Integer [] row: test)
        {
            matrix.add(Arrays.asList(row));
        }
        
        //List <Integer> test = new ArrayList<Integer>(Arrays.asList({1,2,3});
        
        //[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
        //[[10, 20], [30, 40]]
        //[[11], [22], [33]]
        //[[1, 2], [3, 4]]
        
        transposeMatrix(matrix);
    }
    
    public static List<List<Integer>> transposeMatrix(List<List<Integer>> matrix) 
    {
        List<Integer> temp = new ArrayList<Integer>();
        int size=0;
        int matrixSize=matrix.size();
        int posInList=0;
        int numElementsInList=0;
        int counter=0;
        int i;
        int position=0;
        boolean hasTransposed=false;
        //boolean hasStoredIntoMatrix=false;

        //this gets number items in list
        for (List<Integer> ee: matrix)
        {
            for (int k: ee)
            {
                numElementsInList++;
            }
            break;
        }

        //we will now go through each innerList element
        //This is simplest technique
        
        //we know the length innerList elements are identical
        //so sufficient to perform this
        for (int j=0; j<numElementsInList;j++)
        {
            System.out.println("--------Commencing transposing at index: " + position);
            counter=0;
            System.out.println("1CURRENT TRANSFORMED MATRIX: " + finalMatrix);
            //hasStoredIntoMatrix=false;
            /*
            //(step increase is numItemsInList)
            //we know for example, the inner loop indexes should be incrementing as such
            (0,2,4)  for  {1,4}, {2,5}, {3,6}

            //we know for example, the inner loop should be incrementing as such
            (0,3,6)  for  {1,4,7}, {2,5,8}, {3,6,9}
                */
                //ll.add(matrix.get(j));

            for (int k=0; k<matrixSize; k++)
            {
                hasTransposed=false;
                System.out.println("IS IT HERE AFTER BREAK");            
                System.out.println("INNER LIST: " + matrix.get(k));

                //we now need to go throug each element in k
            
                //for (int l=0; l<numElementsInList;l++)
                for (int l: matrix.get(k))
                {
                    if (!hasTransposed)
                    {
                        //we know if l is equal to k
                        //k is each index in numItemsInList
                        //we will be storing these index values for all the List<Integer> 

                        System.out.println("position: " + position);
                        System.out.println("counter: " + counter);

                        if (counter==position)
                        {
                            System.out.println("EVER REACH");
                            temp.add(l);
                            System.out.println("CURRENT LIST: " + temp);
                            System.out.println("1position: " + position + " counter: " + counter);
                            hasTransposed=true;
                            counter=0;
                            //break;
                        }
                        else
                        {
                            counter++;
                        }
                    }                
                }
            
                System.out.println("HERE AFTER BREAK");
                //position++;

                //we need to set position back to 0
                //so that in next iteration, the value of l
                //will reflect the next Integer in List<Integer>
                //position=0;
                //counter++;
            }
            position++;
            //counter++;
            //if (!hasStoredIntoMatrix)
            //{
            System.out.println("2CURRENT TRANSFORMED MATRIX: " + finalMatrix);
            finalMatrix.add(temp);
            System.out.println("NEWLY TRANSFORMATION: " + finalMatrix);
                //hasStoredIntoMatrix=true;
            System.out.println("Clearing current transposed entry from temp storage");
            temp.clear();
            //}
        } //this is the end of the outer for loop

        return finalMatrix;
    }
}