Java Array Sum sum(short[] ary)

Here you can find the source of sum(short[] ary)

Description

Sum of an array

License

Open Source License

Parameter

Parameter Description
ary the array of integers to sum

Return

the sum of all of the elements in the specified array

Declaration

public static final int sum(short[] ary) 

Method Source Code

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

public class Main {
    /** Sum of an array/*from w ww .jav a 2s . c om*/
     * @param ary the array of integers to sum
     * @return the sum of all of the elements in the specified array
     */
    public static final int sum(byte[] ary) {
        return sum(ary, 0, ary.length);
    }

    /** Sum of the subset of an array
     * @param ary the array of bytes to sum
     * @param offset the offset into the array at which to start summing values
     * @param length the number of elements to sum from the array starting
     * at {@code offset}
     * @return the sum of the specified array subset
     */
    public static final int sum(byte[] ary, int offset, int length) {
        int offLen = offset + length;
        int sum = (byte) 0;
        for (int i = offset; i < offLen; i++) {
            sum += ary[i];
        }
        return sum;
    }

    /** Sum of an array
     * @param ary the array of integers to sum
     * @return the sum of all of the elements in the specified array
     */
    public static final int sum(short[] ary) {
        return sum(ary, 0, ary.length);
    }

    /** Sum of the subset of an array
     * @param ary the array of shorts to sum
     * @param offset the offset into the array at which to start summing values
     * @param length the number of elements to sum from the array starting
     * at {@code offset}
     * @return the sum of the specified array subset
     */
    public static final int sum(short[] ary, int offset, int length) {
        int offLen = offset + length;
        int sum = (short) 0;
        for (int i = offset; i < offLen; i++) {
            sum += ary[i];
        }
        return sum;
    }

    /** Sum of an array
     * @param ary the array of integers to sum
     * @return the sum of all of the elements in the specified array
     */
    public static final int sum(char[] ary) {
        return sum(ary, 0, ary.length);
    }

    /** Sum of the subset of an array
     * @param ary the array of chars to sum
     * @param offset the offset into the array at which to start summing values
     * @param length the number of elements to sum from the array starting
     * at {@code offset}
     * @return the sum of the specified array subset
     */
    public static final int sum(char[] ary, int offset, int length) {
        int offLen = offset + length;
        int sum = (char) 0;
        for (int i = offset; i < offLen; i++) {
            sum += ary[i];
        }
        return sum;
    }

    /** Sum of an array
     * @param ary the array of integers to sum
     * @return the sum of all of the elements in the specified array
     */
    public static final int sum(int[] ary) {
        return sum(ary, 0, ary.length);
    }

    /** Sum of the subset of an array
     * @param ary the array of ints to sum
     * @param offset the offset into the array at which to start summing values
     * @param length the number of elements to sum from the array starting
     * at {@code offset}
     * @return the sum of the specified array subset
     */
    public static final int sum(int[] ary, int offset, int length) {
        int offLen = offset + length;
        int sum = 0;
        for (int i = offset; i < offLen; i++) {
            sum += ary[i];
        }
        return sum;
    }

    /** Sum of an array
     * @param ary the array of integers to sum
     * @return the sum of all of the elements in the specified array
     */
    public static final long sum(long[] ary) {
        return sum(ary, 0, ary.length);
    }

    /** Sum of the subset of an array
     * @param ary the array of longs to sum
     * @param offset the offset into the array at which to start summing values
     * @param length the number of elements to sum from the array starting
     * at {@code offset}
     * @return the sum of the specified array subset
     */
    public static final long sum(long[] ary, int offset, int length) {
        int offLen = offset + length;
        long sum = 0L;
        for (int i = offset; i < offLen; i++) {
            sum += ary[i];
        }
        return sum;
    }

    /** Sum of an array
     * @param ary the array of integers to sum
     * @return the sum of all of the elements in the specified array
     */
    public static final float sum(float[] ary) {
        return sum(ary, 0, ary.length);
    }

    /** Sum of the subset of an array
     * @param ary the array of floats to sum
     * @param offset the offset into the array at which to start summing values
     * @param length the number of elements to sum from the array starting
     * at {@code offset}
     * @return the sum of the specified array subset
     */
    public static final float sum(float[] ary, int offset, int length) {
        int offLen = offset + length;
        float sum = 0f;
        for (int i = offset; i < offLen; i++) {
            sum += ary[i];
        }
        return sum;
    }

    /** Sum of an array
     * @param ary the array of integers to sum
     * @return the sum of all of the elements in the specified array
     */
    public static final double sum(double[] ary) {
        return sum(ary, 0, ary.length);
    }

    /** Sum of the subset of an array
     * @param ary the array of doubles to sum
     * @param offset the offset into the array at which to start summing values
     * @param length the number of elements to sum from the array starting
     * at {@code offset}
     * @return the sum of the specified array subset
     */
    public static final double sum(double[] ary, int offset, int length) {
        int offLen = offset + length;
        double sum = 0;
        for (int i = offset; i < offLen; i++) {
            sum += ary[i];
        }
        return sum;
    }
}

Related

  1. sum(Number... numbers)
  2. sum(Number[] array)
  3. sum(Number[] numbers)
  4. sum(Object[] srcOne, Object[] srcTwo)
  5. sum(short tab[], int a, int b)
  6. sum(String[] tokens, int start, int length, String separator)
  7. sum3(double[] a, double[] b)
  8. sum_configurations(int[] elements, int sum)
  9. sumabs(double[] in)