java实例--每日一练

数组获取最大和最小值 

通过运用Collections类的Collections.max() 和 Collections.min() 方法来查找数组中的最大和最小值:

 1 import java.util.Arrays;
 2 import java.util.Collections;
 3  
 4 public class Main {
 5     public static void main(String[] args) {
 6         Integer[] numbers = { 8, 2, 7, 1, 4, 9, 5};
 7         int min = (int) Collections.min(Arrays.asList(numbers));
 8         int max = (int) Collections.max(Arrays.asList(numbers));
 9         System.out.println("最小值: " + min);
10         System.out.println("最大值: " + max);
11     }
12 }

 

以上代码运行输出结果为:

最小值: 1
最大值: 9

 

posted @ 2022-04-08 15:51  future5  阅读(22)  评论(0)    收藏  举报