这个问题在这里已有答案:
如果我想要一个我想要初始化为无穷大的目标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
返回?
这似乎是多余的,不确定是否有更好的方法来做到这一点?
您可以使用Integer.MAX_VALUE
就是这个例子。不需要无限。毕竟,最小值不能高于Integer.MAX_VALUE
。
int min_so_far = Integer.MAX_VALUE;