如何使用 Java 查找 Arraylist 的最大值及其两个索引位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10437623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 01:02:55  来源:igfitidea点击:

How to find the max value of an Arraylist and two of its Index position using Java

javaarraylist

提问by Karthi

How can I find the maximum value from an Arraylistwith its index positions?

如何从Arraylist带有索引位置的 a 中找到最大值?

ArrayList ar = new ArrayList();
ar.add(2); // position 0
ar.add(4); // position 1
ar.add(12); // position 2
ar.add(10); // position 3
ar.add(12); // position 4

String obj = Collections.max(ar);
int index = ar.indexOf(obj);

System.out.println("obj max value is " + obj + " and index position is " + index);

The above program just returns the output as the first max object with value 12and index position 2.

上面的程序只是将输出作为第一个具有 value12和 index position 的max 对象返回2

But my actual output should be index positions 2and 4(because max value 12is present in two index position).

但我的实际输出应该是索引位置24(因为最大值12出现在两个索引位置)。

回答by Théo Moulia

You can use Collectionsto find the max value of a list, and then use the property indexOfto find its position in your list.

您可以使用Collections来查找列表的最大值,然后使用属性indexOf来查找它在您的列表中的位置。

List<Integer> myList = new ArrayList<Integer>();
myList.add(3); // adding some values
myList.add(5);
myList.add(7);
myList.add(3);
myList.add(1);

Integer maxVal = Collections.max(myList); // should return 7
Integer maxIdx = myList.indexOf(maxVal); // should return 2 (position of the value 7)

回答by Timofey Gorshkov

Since Java 8 this could be done by:

从 Java 8 开始,这可以通过以下方式完成:

int index = IntStream.range(0, ar.size()).boxed()
        .max(Comparator.comparing(ar::get)).orElse(-1);

回答by Jigar Joshi

Just iterate once through list, have another list to push index of maximum number

只需遍历列表一次,再使用另一个列表来推送最大数量的索引

回答by maerics

Untested:

未经测试:

public static int maxIndex(List<Integer> list) {
  Integer i=0, maxIndex=-1, max=null;
  for (Integer x : list) {
    if ((x!=null) && ((max==null) || (x>max))) {
      max = x;
      maxIndex = i;
    }
    i++;
  }
  return maxIndex
}
// ...
maxIndex(Arrays.asList(1, 2, 3, 2, 1)); // => 2
maxIndex(Arrays.asList(null, null)); // => -1
maxIndex(new ArrayList<Integer>()); // => -1

回答by Parmesh

public static void main(String[] args){

公共静态无效主(字符串 [] args){

    List<Integer> ar=new ArrayList<Integer>();
    ar.add(2); `adding the values in the list`
    ar.add(5);
    ar.add(6);
    ar.add(4);
    ar.add(6);
    ar.add(5);
    ar.add(6);

    int max=Collections.max(ar);

    while(ar.contains(max))
    {
        int i=ar.indexOf(max); 
        System.out.println("the index of 6:"+ar.indexOf(max));
        ar.set(i, -1);
        System.out.println(ar);
    }       

}

}