Java Array Sum sum(final int[] values)

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

Description

Computes the sum of an array of integers.

License

Open Source License

Parameter

Parameter Description
values the array to be summed, cannot be <code>null</code>

Return

the sum of all values in the array

Declaration

public static int sum(final int[] values) 

Method Source Code

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

public class Main {
    /**// www . j a v  a  2s  .c  om
     * Computes the sum of an array of integers.
     * <p>
     * Does not check for overflow of the result.
     * 
     * @param values the array to be summed, cannot be <code>null</code>
     * @return the sum of all values in the array
     */
    public static int sum(final int[] values) {
        int t = 0;
        for (int i : values) {
            t += i;
        }
        return t;
    }

    /**
     * Computes the sum of an array of longs.
     * <p>
     * Does not check for overflow of the result.
     * 
     * @param values the array to be summed, cannot be <code>null</code>
     * @return the sum of all values in the array.
     */
    public static long sum(final long[] values) {
        int t = 0;
        for (long i : values) {
            t += i;
        }
        return t;
    }
}

Related

  1. sum(final double[] target)
  2. sum(final double[] vec)
  3. sum(final float[] x)
  4. sum(final int base, final int[] ints)
  5. sum(final int[] terms, final int start, final int len)
  6. sum(final int[] values, final int... indice)
  7. sum(final long... values)
  8. sum(float[] a, int off, int length)
  9. sum(float[] array)