Here you can find the source of sum(int[] a)
Parameter | Description |
---|---|
a | a parameter |
public static int sum(int[] a)
//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; } }