Computes the arithmetic mean of a double array. The average is calculated as the sum of all elements divided by the number of elements: avg = Σ(numbers[i]) / n. @param numbers a non-null, non-empty array of double values @return the arithmetic mean of the given numbers @
(double[] numbers)
| 30 | * @throws IllegalArgumentException if {@code numbers} is {@code null} or empty |
| 31 | */ |
| 32 | public static double average(double[] numbers) { |
| 33 | if (numbers == null || numbers.length == 0) { |
| 34 | throw new IllegalArgumentException("Numbers array cannot be empty or null"); |
| 35 | } |
| 36 | double sum = 0; |
| 37 | for (double number : numbers) { |
| 38 | sum += number; |
| 39 | } |
| 40 | return sum / numbers.length; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Computes the arithmetic mean of an {@code int} array. |
no outgoing calls