mina_mz mina_mz - 4 months ago 12
Java Question

How to get values from matrix using arraylist JAVA

I have this matrix:

A B C D E
0 0 0 1 0 -> index 0 in arrList
0 0 1 1 0 -> index 1 in arrList
0 0 1 0 0 -> index 2 in arrList
1 0 1 0 0 -> index 3 in arrList


So in ArrayList arrList contains: [[0,0,0,1,0], [0,0,1,1,0], [0,0,1,0,0], [1,0,1,0,0]]

How do I get the values in column A, B, C, D and E from the arrList in JAVA to get as the Example output?

Example output:
A: [0,0,0,1]
B: [0,0,0,0]
C: [0,1,1,1]
D: [1,1,0,0]
E: [0,0,0,0]


Please help. Thank you.

Answer
for(int row = 0; row < 4; row++){
    for(int col = 0; col < 5; col++){
        System.out.print(arrList[col][row]);
    }
    System.out.println();
}

This will give you the output you need.

P.S.: Do the editing in the code to get the output in the format you would like.