Java中数组获取最大和最小值

谁践踏了优雅 2021-09-28 13:44 455阅读 0赞

Java中数组获取最大和最小值
方法:
通过使用Collection类的Collection.max()和Collection.min()方法来查找数组中的最大和最小值
代码:

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

结果:
Java中数组获取最大和最小值的结果

发表评论

表情:
评论列表 (有 0 条评论,455人围观)

还没有评论,来说两句吧...

相关阅读