【问题标题】:How to get values from matrix using arraylist JAVA如何使用arraylist JAVA从矩阵中获取值
【发布时间】:2016-07-16 09:22:12
【问题描述】:

我有这个矩阵:

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 

所以在ArrayList arrList中包含:[[0,0,0,1,0], [0,0,1,1,0], [0,0,1,0,0], [ 1,0,1,0,0]]

如何从 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]

请帮忙。谢谢。

【问题讨论】:

    标签: java matrix arraylist


    【解决方案1】:

    花哨的 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())));
        }
    }
    

    输出

    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]
    

    作为一个大单行,因为为什么不

    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
    

    【讨论】:

    • @mina_mz 你的索引倒退了。应该是System.out.print(arrList.get(i).get(j)
    【解决方案2】:
    for(int row = 0; row < 4; row++){
        for(int col = 0; col < 5; col++){
            System.out.print(arrList[col][row]);
        }
        System.out.println();
    }
    

    这将为您提供所需的输出。

    P.S.:在代码中进行编辑以获得您想要的格式的输出。

    【讨论】:

      【解决方案3】:

      如果这是一个数组的 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
      }
      

      如果你的意思是使用数组数组:

      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
      }
      

      【讨论】:

        【解决方案4】:
        for(List<Integer> row : arrList) {
            for(int col : row) {
               System.out.print(col + " ");
            }
            System.out.println();
        }
        

        希望这对你有用。您还可以进行一些映射以获得 A、B、C。并添加其他格式。

        干杯。

        【讨论】:

          【解决方案5】:

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

          【讨论】:

          • 列表索引从 0 开始。您的第一条语句获取第二行的第 5 列,您的第二条语句获取第 (i+1) 列的第 (j+1) 行
          • 是的,这是我的错误
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多