简体   繁体   English

如何使用arraylist JAVA从矩阵中获取值

[英]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]]所以在 ArrayList arrList 包含: [[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?如何从 JAVA 中的 arrList 获取 A、B、C、D 和 E 列中的值以作为示例输出?

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.谢谢。

Fancy Java 8 solution花式 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]

As one big one-liner 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

If this is an ArrayList of Arrays use:如果这是数组的 ArrayList 使用:

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
}

If what you meant an array of array use:如果您的意思是数组数组,请使用:

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
}
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.这将为您提供所需的输出。

PS: Do the editing in the code to get the output in the format you would like. PS:在代码中进行编辑以获得您想要的格式的输出。

for(List<Integer> row : arrList) {
    for(int col : row) {
       System.out.print(col + " ");
    }
    System.out.println();
}

Hope this works for you.希望这对你有用。 You could also make some mappings in order to have A, B, C. And add aditional formatting.您还可以进行一些映射以获得 A、B、C。并添加其他格式。

Cheers.干杯。

System.out.println(arrList.get(1).get(4)+" "); System.out.println(arrList.get(1).get(4)+" "); System.out.println(arrList.get(i).get(j)+" "); System.out.println(arrList.get(i).get(j)+" ");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM