Java Array Sum sum(long[] array)

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

Description

Returns the sum of all elements in the array.

License

Open Source License

Parameter

Parameter Description
array Input array

Return

Sum of all array elements

Declaration

public static double sum(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:/*  ww w.  j  av a2s . c om*/
 *     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 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. sum(Integer... integers)
  2. sum(Integer... ints)
  3. sum(Integer[] array)
  4. sum(long array[])
  5. sum(long... values)
  6. sum(long[] array)
  7. sum(long[] counts)
  8. sum(Number... numbers)
  9. sum(Number[] array)