为了账号安全,请及时绑定邮箱和手机立即绑定

如何在整数数组中使用 Math.min 和 Math.max

如何在整数数组中使用 Math.min 和 Math.max

大话西游666 2021-06-09 17:53:29
您可以在此处查看运算符优先顺序。当您执行程序时,它将从左到右开始运行。调用时 m1(),您分配a=2并返回此值。所以在 a = 2 之后你的等式如下:9 + 3 + 3 * 2 + 2 * 2 + 1 + 29 + 3 + 6 + 4 + 1 + 225
查看完整描述

3 回答

?
宝慕林4294392

TA贡献2021条经验 获得超8个赞

这些函数只需要两个参数。如果你想要数组的最小值,你可以使用 IntStream。


int[] a = { 1, 5, 6 };

int max = IntStream.of(a).max().orElse(Integer.MIN_VALUE);

int min = IntStream.of(a).min().orElse(Integer.MAX_VALUE);


查看完整回答
反对 回复 2021-06-17
?
慕姐8265434

TA贡献1813条经验 获得超2个赞

你可以简单地使用,构建JavaCollection和Arrays理清这个问题。您只需要导入它们并使用它。


请检查以下代码。


import java.util.Arrays;

import java.util.Collections;


public class getMinNMax {

    public static void main(String[] args) {


        Integer[] num = { 2, 11, 55, 99 };


        int min = Collections.min(Arrays.asList(num));

        int max = Collections.max(Arrays.asList(num));


        System.out.println("Minimum number of array is : " + min);

        System.out.println("Maximum number of array is : " + max);

    }

}



查看完整回答
反对 回复 2021-06-17
?
守候你守候我

TA贡献1802条经验 获得超10个赞

你可以简单地使用,构建JavaCollection和Arrays理清这个问题。您只需要导入它们并使用它。


请检查以下代码。


import java.util.Arrays;

import java.util.Collections;


public class getMinNMax {

    public static void main(String[] args) {


        Integer[] num = { 2, 11, 55, 99 };


        int min = Collections.min(Arrays.asList(num));

        int max = Collections.max(Arrays.asList(num));


        System.out.println("Minimum number of array is : " + min);

        System.out.println("Maximum number of array is : " + max);

    }

}



查看完整回答
反对 回复 2021-06-17
  • 3 回答
  • 0 关注
  • 232 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信