sum of array elements - Java Collection Framework

Java examples for Collection Framework:Array Element

Description

sum of array elements

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        int[] a = new int[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        System.out.println(sum(a));
    }/*from  w  w  w .  j a  v  a2s. c  om*/

    /**
     * sum of array elements
     * @param a
     * @return
     */
    public static int sum(int[] a) {
        int retval = 0;
        for (int i : a) {
            retval += i;
        }
        return retval;
    }
}

Related Tutorials