July 2017
Beginner to intermediate
715 pages
17h 3m
English
In our first example, we will demonstrate a basic way to calculate mean using standard Java capabilities. We will use an array of double values called testData:
double[] testData = {12.5, 18.7, 11.2, 19.0, 22.1, 14.3, 16.9, 12.5, 17.8, 16.9};
We create a double variable to hold the sum of all of the values and a double variable to hold the mean. A loop is used to iterate through the data and add values together. Next, the sum is divided by the length of our array (the total number of elements) to calculate the mean:
double total = 0; for (double element : testData) { total += element; } double mean = total / testData.length; out.println("The mean is " + mean);
Our output is as follows:
The mean ...