The most basic way of solving this problem of getting the sum of each integer in an array, is to just loop through each item and accumulate using another variable. Below is a simple example on this:
/** * An example program that implements Java Sum Array as a function using for loop. */ public class ExampleProgram { public static void main(String[] args) { int[] myArray = { 5, 2, 10, 15 }; System.out.println("The sum of the array is: "+ sum(myArray)); } public static int sum(int[] intArray) { int sum = 0; for (int i = 0; i < intArray.length; i++) sum += intArray[i]; return sum; } }
So we created above a function sum where we do a simple for loop to go through each element and accumulate the sum. And then we return the sum to the caller. IF we run this we get:
The sum of the array is: 32
If this does not look good for you, we can simplify the for loop further. See example below.
/** * An example program that uses special for loop. */ public class ExampleProgram { public static void main(String[] args) { int[] myArray = { 5, 2, 10, 15 }; System.out.println("The sum of the array is: "+ sum(myArray)); } public static int sum(int[] intArray) { int sum = 0; for (int item:intArray) sum += item; return sum; } }And we still get the same results.
If you are on a newer version of Java and have support for Java Stream, we can solve calculating the sum of all items in an int array using this also. See below example on how to do this:
import java.util.Arrays; /** * An example program that sums an int array using Java Stream. */ public class ExampleProgram { public static void main(String[] args) { int[] myArray = { 5, 2, 10, 15 }; System.out.println("The sum of the array is: "+ sum(myArray)); } public static int sum(int[] intArray) { return Arrays.stream(intArray).sum(); } }So the sum() is invoked using Java Stream and we get a result with far shorter code. And the result will still be the same, shown below:
The sum of the array is: 32And the sum method only applies to primitive int array. But if have an array of Integer instead, we need to use the code below:
import java.util.Arrays; /** * An example program that sums array of Integer using Java Stream. */ public class ExampleProgram { public static void main(String[] args) { Integer[] myArray = { 5, 2, 10, 15 }; System.out.println("The sum of the array is: " + sum(myArray)); } public static int sum(Integer[] intArray) { return Arrays.stream(intArray).mapToInt(Integer::intValue).sum(); } }So in the example above, the parameter is Integer[] instead of int[]. So we need to invoke intValue on the Integer class to convert properly and be able to call the sum method.
Another approach is to use the IntStream class to also get the sum of an array of in in Java. See below example:
import java.util.stream.IntStream; /** * An example program that sums int array using IntStream. */ public class ExampleProgram { public static void main(String[] args) { int[] myArray = { 5, 2, 10, 15 }; System.out.println("The sum of the array is: " + sum(myArray)); } public static int sum(int[] intArray) { return IntStream.of(intArray).sum(); } }