Java Array Sum sum(double[] data, int i_, int n_)

Here you can find the source of sum(double[] data, int i_, int n_)

Description

Calculates sum of n items up to position i in the array

License

Open Source License

Declaration

public static double sum(double[] data, int i_, int n_) 

Method Source Code

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

public class Main {
    /** Calculates sum of n items up to position i in the array */
    public static double sum(double[] data, int i_, int n_) {
        double total = 0;
        for (int i = i_, n = 0; n < n_; i = i > 0 ? i - 1 : data.length - 1, n++) {
            total += data[i];/*  www. j  a  va  2s.c  o m*/
        }
        //System.out.println("i_=" + i_ + ", n_=" + n_ + ", Total of:\n" + Utils.arrayString(data) + " \n =" + total);
        return total;
    }

    /** Calculates the sum of all items in the array */
    public static double sum(double[] data) {
        return sum(data, 0, data.length);
    }

    /** Calculates the sum of all items in the array */
    public static double sum(double[][] data) {
        double total = 0;
        for (int i = 0; i < data.length; i++) {
            total += sum(data[i]);
        }
        return total;
    }

    /** Calculates the sum of all items in the array */
    public static int sum(int[] data) {
        int total = 0;
        for (int i = 0; i < data.length; i++) {
            total += data[i];
        }
        return total;
    }

    /** Calculates the sum of all items in the array */
    public static int sum(int[][] data) {
        int total = 0;
        for (int i = 0; i < data.length; i++) {
            total += sum(data[i]);
        }
        return total;
    }
}

Related

  1. sum(double[] array)
  2. sum(double[] array)
  3. sum(double[] d)
  4. sum(double[] data)
  5. sum(double[] data)
  6. sum(double[] dbs)
  7. sum(double[] output, double[] a, double[] b)
  8. sum(double[] t)
  9. sum(double[] vec1, double[] vec2)