2013-11-09 78 views
0

我:MAXVALUE 2D ArrayList中的Java

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)); 

回答

1

要獲得行中的最大,這是很容易的,你是在正確的道路。

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

對於列,你可以循環做:

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