Java Array Sum sum(int[] arr)

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

Description

SUM all elements of the specified primitive type array.

License

Open Source License

Parameter

Parameter Description
arr the array to be SUM

Declaration

public static int sum(int[] arr) 

Method Source Code

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

public class Main {

    public static int sum(int[] arr) {
        int sum = 0;
        for (int i = 0, l = arr.length; i < l; i++) {
            sum += arr[i];//from   w  ww. j av a 2s . c o m
        }
        return sum;
    }

    public static double sum(double[] arr) {
        double sum = 0;
        for (int i = 0, l = arr.length; i < l; i++) {
            sum += arr[i];
        }
        return sum;
    }

    public static float sum(float[] arr) {
        float sum = 0;
        for (int i = 0, l = arr.length; i < l; i++) {
            sum += arr[i];
        }
        return sum;
    }

    public static long sum(long[] arr) {
        long sum = 0;
        for (int i = 0, l = arr.length; i < l; i++) {
            sum += arr[i];
        }
        return sum;
    }

    public static short sum(short[] arr) {
        short sum = 0;
        for (int i = 0, l = arr.length; i < l; i++) {
            sum += arr[i];
        }
        return sum;
    }

    public static byte sum(byte[] arr) {
        byte sum = 0;
        for (int i = 0, l = arr.length; i < l; i++) {
            sum += arr[i];
        }
        return sum;
    }
}

Related

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