Java Array Sum sum(int[] a)

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

Description

Return the sum of a one dimensional array.

License

Open Source License

Parameter

Parameter Description
a a parameter

Return

The sum of a one dimensional array.

Declaration

public static int sum(int[] a) 

Method Source Code

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

public class Main {
    /**/*  w ww .  j  a  v  a  2s  .c  o  m*/
     * Return the sum of a one dimensional array.
     * 
     * @param a
     * @return The sum of a one dimensional array.
     */
    public static int sum(int[] a) {
        int rowNum = a.length;
        int sum = 0;
        for (int i = 0; i < rowNum; i++) {
            sum += a[i];
        }
        return sum;
    }

    /**
     * Return the sum of all values in an array.
     * 
     * @param a
     * @return The sum of all values in an array.
     */
    public static int sum(int[][] a) {
        int rowNum = a.length;
        int colNum = a[0].length;
        int sum = 0;
        for (int i = 0; i < rowNum; i++) {
            for (int j = 0; j < colNum; j++) {
                sum += a[i][j];
            }
        }
        return sum;
    }
}

Related

  1. sum(float[] distribution)
  2. sum(float[] items, int offset, int length)
  3. sum(float[] vector)
  4. sum(int... values)
  5. sum(int... xs)
  6. sum(int[] a)
  7. sum(int[] a)
  8. sum(int[] a)
  9. sum(int[] add)