Java Array Average avg(double[] values)

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

Description

Calculates the average over an given array of doubles.

License

Open Source License

Parameter

Parameter Description
values double array the average is calculated from

Return

average value of the given double array

Declaration

public static double avg(double[] values) 

Method Source Code

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

public class Main {
    /**//  www .j a  v a2s  .c  om
     * Calculates the average over an given array of doubles.
     * 
     * @param values
     *            double array the average is calculated from
     * @return average value of the given double array
     */
    public static double avg(double[] values) {
        double avg = 0;
        for (double v : values) {
            avg += v;

        }
        return avg / values.length;
    }

    /**
     * Calculates the average over an given array of integers.
     * 
     * @param values
     *            integer array the average is calculated from
     * @return average value of the given integer array
     */
    public static double avg(int[] values) {
        double avg = 0;
        for (int v : values) {
            avg += v;

        }
        return avg / values.length;
    }

    /**
     * Calculates the average over an given array of longs.
     * 
     * @param values
     *            long array the average is calculated from
     * @return average value of the given long array
     */
    public static double avg(long[] values) {
        double avg = 0;
        for (long v : values) {
            avg += v;

        }
        return avg / values.length;
    }
}

Related

  1. avg(double values[], int size, boolean ignoreNan)
  2. avg(double... a)
  3. avg(double[] a)
  4. avg(double[] array)
  5. avg(double[] nums)
  6. avg(double[] values)
  7. avg(double[] x, int start, int end)
  8. avg(final double a, final double b)
  9. avg(final double... values)