简体   繁体   中英

maxvalue 2d arraylist java

I have:

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

How do I find the maximum value at column first,second,third and row first,second,third?

This is what I've tried:

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

To get the max in the rows this is pretty easy and you were in the right way.

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

For the columns you can do a for loop :

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

Output :

7
8
9

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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