Java Array Sum sum(int[] a)

Here you can find the source of sum(int[] a)

Description

sum

License

Open Source License

Parameter

Parameter Description
a a parameter

Return

the sum of the values in a

Declaration

public static int sum(int[] a) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * OscaR is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2.1 of the License, or
 * (at your option) any later version./*from   w  ww .j a  v  a 2  s .  c  o  m*/
 *   
 * OscaR is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License  for more details.
 *   
 * You should have received a copy of the GNU Lesser General Public License along with OscaR.
 * If not, see http://www.gnu.org/licenses/lgpl-3.0.en.html
 ******************************************************************************/

public class Main {
    /**
     *
     * @param a
     * @return the sum of the values in a
     */
    public static int sum(int[] a) {
        int v = 0;
        for (int i : a) {
            v += i;
        }
        return v;
    }

    /**
     *
     * @param a
     * @return the sum of the values in a
     */
    public static double sum(double[] a) {
        double v = 0;
        for (double i : a) {
            v += i;
        }
        return v;
    }

    /**
     *
     * @param a
     * @return the sum of the values in a
     */
    public static int sum(int[][] a) {
        int s = 0;
        for (int i = 0; i < a.length; i++) {
            s += sum(a[i]);
        }
        return s;
    }
}

Related

  1. sum(int... values)
  2. sum(int... xs)
  3. sum(int[] a)
  4. sum(int[] a)
  5. sum(int[] a)
  6. sum(int[] add)
  7. sum(int[] arr)
  8. sum(int[] arr)
  9. sum(int[] arr)