Transpose ArrayList<ArrayList<String>> in Java

2015-01-21T08:33:28

I have a method attempting to transpose a ArrayList containing an ArrayList of string, called matrix and return the new array.

I found Transposing Values in Java 2D ArrayList, but it looks like it's for Arrays and not ArrayLists. My 2D array is of unknown dimensions, either rectangular or possibly irregular (but never square).

My idea was to read each inner array, and append the items to the inner arrays of the outgoing matrix.

public static ArrayList<ArrayList<String>> transpose (ArrayList<ArrayList<String>> matrixIn){
    ArrayList<ArrayList<String>> matrixOut = new ArrayList<>();
    //for each row in matrix
    for (int r = 0; r < matrixIn.size(); r++){
        ArrayList<String> innerIn = matrixIn.get(r);

        //for each item in that row
        for (int c = 0; c < innerIn.size(); c++){

            //add it to the outgoing matrix

            //get matrixOut current value
            ArrayList<String> matrixOutRow = matrixOut.get(c);
            //add new one
            matrixOutRow.add(innerIn.get(c));
            //reset to matrixOut
            matrixOut.set(c,matrixOutRow);
        }
    }
    return matrixOut;
}

I'm getting an "IndexOutOfBoundsException: Index: 0, Size: 0" error at

        //get matrixOut[v]
        ArrayList<String> matrixOutRow = matrixOut.get(v);

What am I doing wrong with this thing?

Copyright License:
Author:「the_dover」,Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.
Link to:https://stackoverflow.com/questions/28057683/transpose-arraylistarrayliststring-in-java

About “Transpose ArrayList<ArrayList<String>> in Java” questions

I have a method attempting to transpose a ArrayList containing an ArrayList of string, called matrix and return the new array. I found Transposing Values in Java 2D ArrayList, but it looks like i...
I want to transpose my list Assume I am having the studentList List&lt;Student&gt; studentList = new ArrayList&lt;Student&gt;(); studentList.add(new Student(101, "English", "95")); studentList
I'm wonder how can I undo my transpose operation. Let me be more specific in example: a = np.random.rand(25,32,11) b = a.transpose(2,0,1) c = b.transpose(??) ### Here I should set (1,0,2) # c == a
I have a model that i want to port to tflite micro. However, when i run the code it gives me the following error: Didn't find op for builtin opcode 'TRANSPOSE' version '2' Failed to get registration
How could I apply the transpose operator after it has returned the nominal expression? I'm working with some symbolic matrix equations, and I have to deal with the transpose, after that I need to
What is the difference between ndarray.transpose and numpy.transpose ? and in which scenario, we have to which one ? is there a scenario where only one of the above will work ? I have gone through...
Some background for the question: I'm trying to optimise a custom neural network code. It relies heavily on loops and I decided to use cython to speed up the calculation. I followed the usual onl...
Looking for a more appropriate approach. I have a working solution, but it seems there should be a built-in or more elegant method. I am comparing two sheets from separate workbooks, documenting the
I have several huge textfiles which I need to transpose. I get stuck on the idea using Array::Transpose for that purpose - but somehow I can't come to an end... Here is my source code: #!/usr/bin/...
I have a question about transposing data without using PROC Transpose. 0 a b c 1 dog cat camel 2 9 7 2534 Without using PROC TRANSPOSE, how can I get a resulting datas...

Copyright License:Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.