Java Array Average getAverage(double[] values)

Here you can find the source of getAverage(double[] values)

Description

Calculate mean

License

Open Source License

Parameter

Parameter Description
values the values to use

Return

mean

Declaration

public static double getAverage(double[] values) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.Collection;

public class Main {
    /**/*from   w  ww .  j  a  va2s.c o  m*/
     * Calculate mean
     * @param values the values to use
     * @return mean
     */
    public static double getAverage(double[] values) {
        return sum(values) / (double) values.length;
    }

    public static double getAverage(Collection<Double> values) {
        return sum(values) / (double) values.size();
    }

    public static double sum(double[] values) {
        double sum = 0.0;
        for (int i = 0; i < values.length; i++) {
            sum += values[i];
        }
        return sum;
    }

    /**
     * Calculate sum
     * @param values
     * @return
     */
    public static double sum(Collection<Double> values) {

        double sum = 0.0;

        for (Double value : values) {
            sum += value;
        }

        return sum;
    }
}

Related

  1. avgArray(double[] values, int maxLength)
  2. avgArrayAndDouble(double[] array, double doublevalue)
  3. avgIgnoreNaN(double[] values)
  4. avgOfArray(double[] array)
  5. avgRng(double[] high, double[] low, int days)
  6. getAverage(int[] values)
  7. zero(double[][] A)