Java Array Average average(long[] array)

Here you can find the source of average(long[] array)

Description

Returns the average value of an array.

License

Open Source License

Parameter

Parameter Description
array Input array

Return

Average value

Declaration

public static double average(long[] array) 

Method Source Code

//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  ww  .j  av  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
 ******************************************************************************/

public class Main {
    /**
     * Returns the average value of an array.
     *
     * @param array Input array
     * @return Average value
     * 
     */
    public static double average(long[] array) {
        return array.length == 0 ? 0 : sum(array) / array.length;
    }

    /**
     * Returns the sum of all elements in the array.
     *
     * @param array Input array
     * @return Sum of all array elements
     * 
     */
    public static double sum(long[] array) {
        double out = 0;
        for (int i = 0; i < array.length; i++) {
            out += array[i];
        }

        return out;
    }

    /**
     * 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(long[] array1, long[] array2) {
        double[] out = new double[array1.length];
        for (int i = 0; i < out.length; i++)
            out[i] = array1[i] + array2[i];

        return out;
    }
}

Related

  1. average(int[] a)
  2. average(int[] array)
  3. average(int[] pixels)
  4. average(int[] pixels)
  5. average(int[] x)
  6. average(long[] vals)
  7. average(Number[] numbers)
  8. average1(double[] d)
  9. average2(double[] d)