在java中使用Infinity以避免双重类型的最佳方法? [重复]

问题描述 投票:1回答:1

这个问题在这里已有答案:

如果我想要一个我想要初始化为无穷大的目标Integer数字,我是否被迫使用Double类型开头?

Integer min_val(List<Integer> nums) {

    double min_so_far = Double.POSITIVE_INFINITY;

    for (Integer i : nums) {
        if (i < min_so_far) {
            min_so_far = (double) i;
        }
    }
    return (int) min_so_far;
}

例如,上面的min函数,我正在寻找List<Integer>中的最小整数。我必须开始使用min_so_far作为double,然后强制将int中的每个nums转换为double,然后将其转换回int返回?

这似乎是多余的,不确定是否有更好的方法来做到这一点?

java integer double
1个回答
3
投票

您可以使用Integer.MAX_VALUE就是这个例子。不需要无限。毕竟,最小值不能高于Integer.MAX_VALUE

int min_so_far = Integer.MAX_VALUE;
© www.soinside.com 2019 - 2024. All rights reserved.