If I want to have a target Integer
number that I want to initialized as infinity, Am I forced to use the Double
type to begin with?
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;
}
For example, this min
function above, I was looking for the minimum integer in a List<Integer>
. I have to started with the min_so_far
as double
, then force convert every int
in the nums
to double
, and then convert it back to int
for return?
It seems quite redundant, not sure if there is a better way to do this?