java 你如何找到一个 ArrayList 的最小值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36211028/
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-11-03 01:04:48  来源:igfitidea点击:

How do you find the smallest value of an ArrayList?

javaarraylist

提问by Austin Brown

I have an array list called children that has objects that move around certain whole value distances in the GUI and I need to find the smallest of these values and print it to the console.

我有一个名为 children 的数组列表,它的对象在 GUI 中围绕某些整数值距离移动,我需要找到这些值中的最小值并将其打印到控制台。

I'm pretty sure this calls for a for loop right? I just don't know how to structure it correctly to make sure it looks at the values within the "children" ArrayList. Or is there some built in functionality within Eclipse to calculate the smallest value of an array list?

我很确定这需要一个 for 循环,对吗?我只是不知道如何正确构建它以确保它查看 "children" 中的值ArrayList。或者 Eclipse 中是否有一些内置功能来计算数组列表的最小值?

回答by TheMirrox

The Collection class has static methods for finding the smallest or the largest element in a Collection. For example:

Collection 类具有用于查找 Collection 中最小或最大元素的静态方法。例如:

 System.out.println("max: " + Collections.max(list));
 System.out.println("min: " + Collections.min(list));

回答by dlicheva

If you have and array of positive integer values you can find the smallest one with the following logic:

如果您有正整数值的数组,则可以使用以下逻辑找到最小的值:

int [] numbers = {10, 20, 30, 40, 50};
int smallest = numbers[0];
for(int x : numbers ){
   if (x < smallest) {
      smallest = x;
   }
}
System.out.println(smallest);

回答by Laurel

Something like:

就像是:

double smallest= children.get(0);
for(double d: children){
    if(smallest > d) smallest=d;

}

回答by Ambidextrous

You can sort the arraylist using Comparators http://beginnersbook.com/2013/12/java-arraylist-of-object-sort-example-comparable-and-comparator/

您可以使用比较器对数组列表进行排序http://beginnersbook.com/2013/12/java-arraylist-of-object-sort-example-comparable-and-comparator/

Once it is sorted, pick the first value, it will be smallest one

排序后,选择第一个值,它将是最小的

回答by Jeanma

You're right, you must use loops. You need to take a value out of the list an compare it to all other values. If there is no smaller one, it's the smallest. If there is, you need do check the next one. Java code could soething like this:

你是对的,你必须使用循环。您需要从列表中取出一个值并将其与所有其他值进行比较。如果没有更小的,那就是最小的。如果有,您需要检查下一个。Java 代码可能是这样的:

numberLoop: for(int number: children){
    for(int numberToCompare: children){
        if(numberToCompare < number)numberLoop.continue;
    }
    System.out.println(number);
    break;
}

PS: It's not Eclipse that's providing the methods. It's basically just a programm to edit text (like Word). The methods are provided by the java API

PS:提供方法的不是 Eclipse。它基本上只是一个编辑文本的程序(如 Word)。方法由 java API 提供

回答by J.R

Using Java 8 Streams ,

使用 Java 8 流,

Let arr is a array of integers,

令 arr 是一个整数数组,

int[] arr = new int[]{54,234,1,45,14,54};    
int small = Arrays.stream(arr).reduce((x, y) -> x < y ? x : y).getAsInt();
int large = Arrays.stream(arr).reduce((x, y) -> x > y ? x : y).getAsInt();
System.out.println(small);
System.out.println(large);