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 columns A, B, C, D and E from arrList in JAVA to get Example as 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.

+4
source share
4 answers
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 result you need.

PS: Do the editing in the code to get the result in the format you would like.

0
source

Fancy solution for Java 8

import java.util.List;
import java.util.stream.IntStream;

import static java.lang.System.lineSeparator;
import static java.util.Arrays.asList;
import static java.util.stream.Collectors.joining;

public class Example {
    public static void main(String[] args) {
        List<List<Integer>> matrix = asList(
                asList(0, 0, 0, 1),
                asList(0, 0, 0, 0),
                asList(0, 1, 1, 1),
                asList(1, 1, 0, 0),
                asList(0, 0, 0, 0)
        );
        System.out.println("Example output:");
        System.out.println(IntStream.range(0, matrix.size())
                .mapToObj(i -> (char) ('A' + i) + ": [" + matrix.get(i)
                        .stream()
                        .map(String::valueOf)
                        .collect(joining(", "))
                        + "]"
                )
                .collect(joining(lineSeparator())));
    }
}

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]

Like one big single line, because why not

System.out.println(((Function<List<List<Integer>>, String>) (matrix -> "Example output: " + lineSeparator() + range(0, matrix.size()).mapToObj(i -> (char) ('A' + i) + ": [" + matrix.get(i).stream().map(String::valueOf).collect(joining(", ")) + "]").collect(joining(lineSeparator())))).apply(asList(asList(0, 0, 0, 1), asList(0, 0, 0, 0), asList(0, 1, 1, 1), asList(1, 1, 0, 0), asList(0, 0, 0, 0))));
// extra space for scroll
+1
source

, :

for(int i=0;i<arrList.size();i++)
{
    for(int j=0;j<arrList.get(i).length;j++)
    {
        System.out.print(arrList.get(i)[j]+" ");
    }
    System.out.printlm("");//To skip line
}

, :

for(int i=0;i<arrList.length;i++)
{
    for(int j=0;j<arrList[i].length;j++)
    {
        System.out.print(arrList[i][j]+" ");
    }
    System.out.println("");//To skip line
}
0
for(List<Integer> row : arrList) {
    for(int col : row) {
       System.out.print(col + " ");
    }
    System.out.println();
}

Hope this works for you. You can also do some mappings to have A, B, C. And add additional formatting.

Greetings.

0
source

Source: https://habr.com/ru/post/1648084/


All Articles