July 2017
Beginner to intermediate
715 pages
17h 3m
English
In our first example, we will use a basic Java approach to calculate the median. For these examples, we have modified our testData array slightly:
double[] testData = {12.5, 18.3, 11.2, 19.0, 22.1, 14.3, 16.2, 12.5, 17.8, 16.5};
First, we use the Arrays class to sort our data because finding the median is simplified when the data is in numeric order:
Arrays.sort(testData);
We then handle three possibilities:
The following code could be shortened, but we have been explicit to help clarify the process. If our list has an even number of values, we divide the length of the list by 2. The first variable, ...