Aggregating data in an array is a common programming task. This can easily be done in Java by initializing a variable to hold the summed value, looping over the elements in the array, and adding these values to the total. The sumArray method below is a good example of how to sum the values in an array of ints using Java.

int sumArray(int[] arr) {
    int sum = 0;
    for(int i=0; i < arr.length; i++) {
        sum = sum + arr[i];
    return sum;

Leave a Reply

How to Sum Array of Ints in Java