【问题标题】:maxvalue 2d arraylist java最大值二维数组列表java
【发布时间】:2013-11-09 20:52:19
【问题描述】:

我有:

ArrayList<List<Integer>> group = new ArrayList<List<Integer>>();
group.add(Arrays.asList(1, 2, 3));
group.add(Arrays.asList(4, 5, 6));
group.add(Arrays.asList(7, 8, 9));

如何在第一、第二、第三列和第一、第二、第三行找到最大值?

这是我尝试过的:

int aMaxR1, aMaxR2, aMaxR3;
int aMinC1, aMinC2, aMinC3;

aMaxR1 = Collections.min(group.get(here could be first row));
aMaxR2 = Collections.min(group.get(here could be second row));
aMaxR3 = Collections.min(group.get(here could be third row));

aMaxC1 = Collections.max(group.get(here could be first column));
aMaxC2 = Collections.max(group.get(here could be second column));
aMaxC3 = Collections.max(group.get(here could be third column));

【问题讨论】:

    标签: java arraylist max


    【解决方案1】:

    要在行中获得最大值,这非常容易,而且您的方法是正确的。

    aMaxR1 = Collections.max(group.get(0));
    

    对于列,您可以执行 for 循环:

     for(int i = 0; i < group.size(); i++){
         System.out.println(Math.max(Math.max(group.get(0).get(i), group.get(1).get(i)), group.get(2).get(i)));
      }
    

    输出:

    7
    8
    9
    

    【讨论】:

      猜你喜欢
      • 2017-03-16
      • 1970-01-01
      • 1970-01-01
      • 2014-06-28
      • 1970-01-01
      • 2016-06-21
      • 2017-07-23
      • 2015-11-29
      • 1970-01-01
      相关资源
      最近更新 更多