I am attempting to find the smallest and largest numbers, stored as doubles, in an Array. This is the array:
double[] heightArray = {1.4,1.9,1.31,1.2};
In attempting to find the smallest and largest element, I have used the following method:
public static void studentHeights(double[] heightArray) {
double total = 0;
double average;
double maximum = 0;
double minimum = 0;
for (int loop = 0; loop < heightArray.length; loop++) {
System.out.println(heightArray[loop]);
total += heightArray[loop];
if (heightArray[loop] > maximum) {
maximum = heightArray[loop];
}
if (heightArray[loop] < minimum) {
minimum = heightArray[loop];
}
}
average = total / heightArray.length;
System.out.println("The maximum height is: " + maximum);
System.out.println("The minimum height is: " + minimum);
System.out.println("The average height is: " + average);
}
The console outputs:
1.4
1.9
1.31
1.2
The maximum height is: 1.9
The minimum height is: 0.0
The average height is: 1.4525
The largest (maximum) element is correct, as is the average. However, the smallest (minimum) element is being recorded as 0.0
. This must be because none of the elements are less than 0, but I can't think how I can calculate the smallest element another way.
答案 0 :(得分:4)
Set your minimum to be something very large instead of 0
double minimum = 0;
Such as the largest possible value, conveniently defined in a constant.
double minimum = Double.MAX_VALUE;
You may want to use the MIN_VALUE when initializing the maximum value
double maximum = Double.MIN_VALUE;
答案 1 :(得分:4)
You can initialize the minimum variable with the first element of the array instead of the value 0,
for example
double minimum = heightArray[0];
If there are negative numbers in the array your maximum logic will not work as excepted, consider initialize the maximum variable also
double maximum = heightArray[0];
答案 2 :(得分:3)
If you are interested in an alternative method you can use Collections.max()
and Collections.min()
for this task:
List<Double> lst = Arrays.asList(heightArray); // array to list
System.out.println(Collections.min(lst)); // print max value
System.out.println(Collections.max(lst)); // print min value
System.out.println(lst.stream().mapToDouble(e->e.doubleValue()).average().getAsDouble()); //average
Why Collections.max()
or Collections.min()
? Because it can be used in any type of primitive or Wrapper Class Objects.
Links:
答案 3 :(得分:3)
In Java 8 be more easier, you have multiple choices to go with
Option 1
double max = Arrays.stream(heightArray).max().getAsDouble();
double min = Arrays.stream(heightArray).min().getAsDouble();
double avg = Arrays.stream(heightArray).average().getAsDouble();
Option 2
You can use DoubleStream.of(double... values)
double max = DoubleStream.of(heightArray).max().getAsDouble();
double min = DoubleStream.of(heightArray).min().getAsDouble();
double avg = DoubleStream.of(heightArray).average().getAsDouble();
Option 3
You can use DoubleSummaryStatistics
DoubleSummaryStatistics stats = DoubleStream.of(heightArray).summaryStatistics();
double min = stats.getMin();
double max = stats.getMax();
double avg = stats.getAverage();
答案 4 :(得分:2)
Or you could just sort()
your array:
// Initialize unsorted double array.
double HeightsArray[] = {1.4, 1.9, 1.31, 1.2};
// Sorting the array.
Arrays.sort(HeightsArray);
System.out.println("Smallest number: " + HeightsArray[0]);
System.out.println("Largest number: " + HeightsArray[HeightsArray.length - 1]);
Will output:
Smallest number: 1.2
Largest number: 1.9
答案 5 :(得分:1)
The problem, as you've already identified, is that you want to start your iteration with a value larger than any other value in your array. Ideally, we would start with the largest value available to store in a double
. Fortunately, we have just such a value: Double.MAX_VALUE
.
Likewise, you could change your maximum search to start with - Double.MAX_VALUE
so that your function continues working if passed a list of entirely negative numbers.