Java Array Sum sum(double[] array)

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

Description

sum

License

LGPL

Declaration

public static final double sum(double[] array) 

Method Source Code

//package com.java2s;
/*//from w w w  .  ja v a  2 s.  c o  m
This code is licensed under the LGPL v3 or greater with the classpath exception, 
with the following additions and exceptions.  
    
packages cern.* have retained the original cern copyright notices.
    
packages at.mabs.cmdline and at.mabs.util.* 
have the option to be licensed under a BSD(simplified) or Apache 2.0 or greater  license 
in addition to LGPL. 
    
Note that you have permission to replace this license text to any of the permitted licenses. 
    
Main text for LGPL can be found here:
http://www.opensource.org/licenses/lgpl-license.php
    
For BSD:
http://www.opensource.org/licenses/bsd-license.php
    
for Apache:
http://www.opensource.org/licenses/apache2.0.php
    
classpath exception:
http://www.gnu.org/software/classpath/license.html
*/

import java.util.*;

public class Main {
    public static final double sum(List<Double> list) {
        double accumulator = 0;
        if (list instanceof RandomAccess) {
            for (int i = 0; i < list.size(); i++) {
                accumulator += list.get(i);
            }
        } else {
            Iterator<Double> iter = list.iterator();
            while (iter.hasNext())
                accumulator += iter.next();
        }
        return accumulator;
    }

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

    public static final int sum(int[] array) {
        int cumulant = 0;
        for (int i = 0; i < array.length; i++)
            cumulant += array[i];
        return cumulant;
    }

    public static final long sum(long[] array) {
        long cumulant = 0;
        for (int i = 0; i < array.length; i++)
            cumulant += array[i];
        return cumulant;
    }
}

Related

  1. sum(double[] a, double[] b)
  2. sum(double[] aArray)
  3. sum(double[] addends)
  4. sum(double[] array)
  5. sum(double[] array)
  6. sum(double[] array)
  7. sum(double[] array)
  8. sum(Double[] array)
  9. sum(double[] array)