Here you can find the source of average(double[] array)
Parameter | Description |
---|---|
array | Input array |
public static double average(double[] array)
//package com.java2s; /******************************************************************************* * Copyright (c) 2016 Pablo Pavon-Marino. * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU Lesser Public License v2.1 * which accompanies this distribution, and is available at * http://www.gnu.org/licenses/lgpl.html * * Contributors:// w w w .ja v a 2 s.c o m * Pablo Pavon-Marino - Jose-Luis Izquierdo-Zaragoza, up to version 0.3.1 * Pablo Pavon-Marino - from version 0.4.0 onwards ******************************************************************************/ import java.util.*; import java.util.Map.Entry; public class Main { /** * Returns the average value of an array. * * @param array Input array * @return Average value (or zero, if {@code array} is empty) */ public static double average(double[] array) { return array.length == 0 ? 0 : sum(array) / array.length; } /** * Returns the average of the map values. * * @param <A> Key type * @param map Input map * @return Average value (or zero, if {@code map} is empty) */ public static <A> double average(Map<A, Double> map) { return average(map.values()); } /** * Returns the average value of a collection. * * @param collection Input collection * @return Average value (or zero, if {@code collection} is empty) */ public static double average(Collection<Double> collection) { return collection.isEmpty() ? 0 : sum(collection) / collection.size(); } /** * Returns the element-wise sum of two maps. * * @param <A> Key type * @param map1 Input map 1 * @param map2 Input map 2 * @return A new map with the element-wise sum */ public static <A> Map<A, Double> sum(Map<A, Double> map1, Map<A, Double> map2) { Map<A, Double> out = new LinkedHashMap<A, Double>(); for (Entry<A, Double> entry : map1.entrySet()) { A key = entry.getKey(); out.put(key, entry.getValue() + map2.get(key)); } return out; } /** * Returns the sum of all elements in the input array. * * @param array Input array * @return Sum of all array elements */ public static double sum(double[] array) { double out = 0; for (int i = 0; i < array.length; i++) out += array[i]; return out; } /** * Returns the sum of all elements in the input matrix. * * @param matrix Input matrix * @return Sum of all matrix elements */ public static double sum(double[][] matrix) { double out = 0; for (double[] matrix1 : matrix) out += sum(matrix1); return out; } /** * Returns the sum of all elements in the input collection. * * @param collection Input collection * @return Sum of all collection values */ public static double sum(Collection<Double> collection) { double out = 0; for (double value : collection) out += value; return out; } /** * Returns the sum of all elements in the input collection. * * @param <A> Key type * @param map Input map * @return Sum of all map values */ public static <A> double sum(Map<A, Double> map) { return sum(map.values()); } /** * Returns the element-wise sum of two arrays. * * @param array1 Input array 1 * @param array2 Input array 2 * @return A new array with the element-wise sum */ public static double[] sum(double[] array1, double[] array2) { double[] out = new double[array1.length]; for (int i = 0; i < out.length; i++) out[i] = array1[i] + array2[i]; return out; } }