# Java Array Tutorials
# Java Array Programs
➤ Find Length of Array
➤ Different ways to Print Array
➤ Sum of Array Elements
➤ Average of Array Elements
➤ Sum of Two Arrays Elements
➤ Compare Two Arrays in Java
➤ 2nd Largest Number in Array
➤ How to Sort an Array in Java
➤ Reverse an Array in Java
➤ GCD of N Numbers in Java
➤ Linear Search in Java
➤ Binary Search in Java
➤ Copy Array in Java
➤ Merge 2 Arrays in Java
➤ Merge two sorted Arrays
➤ Largest Number in Array
➤ Smallest Number in Array
➤ Remove Duplicates
➤ Insert at Specific Position
➤ Add Element to Array
➤ Remove Element From Array
➤ Count Repeated Elements
➤ More Array Programs
Java Matrix Programs
➤ Matrix Tutorial in Java
➤ Print 2D Array in Java
➤ Print a 3×3 Matrix
➤ Sum of Matrix Elements
➤ Sum of Diagonal Elements
➤ Row Sum – Column Sum
➤ Matrix Addition in Java
➤ Matrix Subtraction in Java
➤ Transpose of a Matrix in Java
➤ Matrix Multiplication in Java
➤ Menu-driven Matrix Operations
Sum of Array in Java | Array Programs in Java – 5 | In the previous Java program, we have seen the different ways to print an array in Java. In this post, we will write a program to find the sum of an array in Java | Sum of Array Elements in Java | Array Addition in Java |
Program Description:- Write a Java program to find the sum of array elements in Java.
Examples of the sum of array elements. Assume we have a given array,
array[]
= {10, 20, 30}
Sum of array elements = 10+20+30 = 60
Similarly,
array[]
= {50, 60, -20, 55, -90}
Sum of array elements = 50+60-20+55-90 = 55
Procedure to find the sum of array elements,
1) Take one array.
2) Declare one sum variable and initialize it with 0.
3) Using a loop, Iterate through the elements of the given array.
4) Add element to the sum variable and assign result value back to the sum variable. (i.e. sum = sum + arr[i]
)
5) When all elements of the array are added to the sum variable then display the result.
Now, let us see the array addition in Java and display the result. First, we will develop a program by hardcoding the values, and again we develop another Java program to take array input from the end-user and then find the sum of array elements. To iterate through the array elements, you can use the length property of the array.
Java program to calculate the sum of array elements
public class ArraySum {
public static void main(String[] args) {
// declare array and
// initialize it with values
int array[] = {10, 20, 30, 40, 50};
// initialize sum variable with 0
int sum = 0;
// add array elements
for (int i=0; i<array.length; i++) {
sum += array[i]; // sum = sum + array[i];
}
// display the result
System.out.println("Sum of array elements= " + sum);
}
}
Output:-
Sum of array elements= 150
In the previous program we have hardcoded the values, now let us develop another Java program to solve the same problem but take input values from the end-user.
Java Program To Calculate The Sum Of Array Elements By Taking Input Values
import java.util.Scanner;
public class ArraySum {
public static void main(String[] args) {
// create Scanner class object
Scanner scan = new Scanner(System.in);
// take size of array
System.out.print("Enter number of elements: ");
int number = scan.nextInt();
// declare array with given size
int array[] = new int[number];
// take input for array elements
System.out.println("Enter array elements: ");
for (int i=0; i<array.length; i++) {
array[i] = scan.nextInt();
}
// initialize sum variable with 0
int sum = 0;
// add array elements to sum
for (int i=0; i<array.length; i++) {
sum += array[i];
}
// display the result
System.out.println("Sum of array elements= " + sum);
}
}
Output for the different test-cases:-
Enter number of elements: 5
Enter array elements:
10 20 30 40 50
Sum of array elements= 150
Enter number of elements: 3
Enter array elements:
150 -100 120
Sum of array elements= 170
In the above program, instead of the array.length you can also use numbers variable because both hold the same value. For simplicity, we are using the Scanner class to take input you can also use BufferReader class. To iterate through the loop we can also use a for-each loop rather than a simple for loop.
// for-each loop
for (int i : list) {
// add array elements to sum
sum += i;
}
Limitation of the above program:- the number of array elements must be known at the program development time. If the number of array elements is dynamic and can’t be predicted before then we must take the help of collection classes because they can grow dynamically.
import java.util.List;
import java.util.ArrayList;
public class ArraySum {
public static void main(String[] args) {
// declare list
List<Integer> list = new ArrayList<Integer>();
// add elements to the list
// you can take input from the end-user
list.add(10);
list.add(20);
list.add(30);
list.add(40);
list.add(50);
// initialize sum variable with 0
int sum = 0;
// add array elements to sum
for (int i : list) {
sum += i;
}
// display the result
System.out.println("Sum = " + sum);
}
}
Output:-
Sum = 150
The Sum of Array Elements in Java Using Stream
We can convert an array to the IntStream using Arrays.stream() method, and IntStream contains the sum() method which gives the sum of array elements.
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
int numbers[] = { 10, 20, 30, 40, 50 };
int sum = Arrays.stream(numbers).sum();
System.out.println(sum); // 150
}
}
But if the numbers are stored in a List then we can use one of the below approaches:-
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
int sum = numbers.stream().reduce(0, (a, b) -> a + b);
System.out.println(sum); // 15
The same can be done using the Integer.sum() method:-
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
int sum = numbers.stream().reduce(0, (a, b) -> Integer.sum(a, b));
// or
int sum = numbers.stream().reduce(0, Integer::sum);
We can also convert the stream into IntStream and call the sum() method:-
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
int sum = numbers.stream().mapToInt(n -> n).sum();
// or
int sum = numbers.stream().mapToInt(Integer::intValue).sum();
We can also use the collect() method with Collectors.summingInt() as follows:-
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
int sum = numbers.stream().collect(Collectors.summingInt(n -> n));
// or
int sum = numbers.stream().collect(Collectors.summingInt(Integer::intValue));
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!