Calculating statistics from a double array

I have an array of double values. I would like to calculate the maximum and average of all the values ​​in this array.

Currently, I'm just looping through an array and calculating numbers, and everything is working fine. But it seems strange to me that I have to implement such basic methods. I searched in the Java API if there is a way that can do something similar, but I could not find ... Am I missing something?

I searched here, but I only found a bunch of examples where they implement it the same way I do.

+3
source share
3 answers

, ​​ Apache commons math. StatUtils, , .

.

:

// Just building an array with double values
int size = 10;
double measurements[] = new double[size];

for (int i = 0; i < measurements.length; i++)
{
    measurements[i] = i;
}

// Using the Apache functions
double myAverage = StatUtils.mean (measurements);
double myMax = StatUtils.max (measurements);
+3

Java 8 (double, long, int), . :

double[] array = {1, 2, 3, 5, 3};
DoubleSummaryStatistics stats = Arrays.stream(array).summaryStatistics();
System.out.println(stats.getMax());
System.out.println(stats.getAverage());
+1

All Articles