Java中数组获取最大和最小值
Java中数组获取最大和最小值
方法:
通过使用Collection类的Collection.max()和Collection.min()方法来查找数组中的最大和最小值
代码:
import java.util.Arrays;
import java.util.Collections;
public class ArrayMaxOrMin {
public static void main(String[] args) {
Integer[] numbers = {8,2,7,1,4,9,5};
int min = (int)Collections.min(Arrays.asList(numbers));
int max = (int)Collections.max(Arrays.asList(numbers));
System.out.println("最小值为:"+min);
System.out.println("最大值为:"+max);
}
}
结果:
还没有评论,来说两句吧...