Calculate the Sum and Average of Elements in an ArrayList in Java
A Dynamic and Adaptable method for storing and managing collections of elements is to use ArrayList. Finding the total and average of an ArrayList’s items is frequently required when working with numerical data that is stored in the list.
In this article, we will see how we can sum and find the average of the ArrayList in Java.
Methods to Calculate the Sum or Average of Elements in an ArrayList
- Using Enhanced for loop
- Using simple for-loop
Program to Calculate the Sum and Average of Elements in an ArrayList in Java
Method 1: Using Enhanced for loop
The following implementation demonstrates how to Sum and find the Average of an ArrayList using for each loop.
Java
// Java program to calculate sum and average of elements in an ArrayList import java.io.*; import java.util.ArrayList; class GFG { public static void main(String[] args) { ArrayList<Integer> list = new ArrayList<>(); list.add( 10 ); list.add( 20 ); list.add( 30 ); list.add( 45 ); list.add( 54 ); // Calculate the sum of elements int sum = 0 ; for ( int num : list) { sum += num; } System.out.println( "Sum: " + sum); // Calculate the average of elements double average = ( double )sum / list.size(); System.out.println( "Average: " + average); } } |
Output
Sum: 159 Average: 31.8
Explanation of the Program:
- In the above program, an
ArrayList
namedlist
is created to store integers. - Integer values are added to the
ArrayList
. - The Sum is calculated using an enhanced for loop (
for-each
loop), where each element is iterated and added to thesum
variable. - Then, the average of elements is calculated by dividing the sum by the number of elements in the list, converted to double to get a more accurate result.
- Finally, the sum and average are printed to the console.
Method 2: Using for loop
The following implementation demonstrates how to Sum and find Average of an ArrayList using simple for loop.
Java
// Java program to calculate sum and average of elements in an ArrayList import java.io.*; import java.util.ArrayList; class Main { public static void main(String[] args) { ArrayList<Integer> list = new ArrayList<>(); list.add( 10 ); list.add( 20 ); list.add( 30 ); list.add( 45 ); list.add( 54 ); // Calculate the sum of elements int sum = 0 ; for ( int i = 0 ; i < list.size(); i++) { sum += list.get(i); } System.out.println( "Sum: " + sum); // Calculate the average of elements double average = ( double )sum / list.size(); System.out.println( "Average: " + average); } } |
Output
Sum: 159 Average: 31.8
Explanation of the Program:
- In the above program, an
ArrayList
namedlist
is created to store integers. - Integer values are added to the
ArrayList
. - The Sum is calculated by iterating through the list and adding each element to the
sum
variable. - Then, the average of elements is calculated by dividing the sum by the number of elements in the list, converted to double to get a more accurate result.
- Finally, the sum and average are printed to the console.
Contact Us