Calculate the Sum and Average of Elements in an ArrayList in Java Last Updated : 12 Feb, 2024 Comments Improve Suggest changes Like Article Like Report 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 ArrayListUsing Enhanced for loopUsing simple for-loopProgram to Calculate the Sum and Average of Elements in an ArrayList in JavaMethod 1: Using Enhanced for loopThe 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); } } OutputSum: 159 Average: 31.8 Explanation of the Program:In the above program, an ArrayList named list 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 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.Method 2: Using for loopThe 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); } } OutputSum: 159 Average: 31.8 Explanation of the Program:In the above program, an ArrayList named list 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. Comment More infoAdvertise with us Next Article Calculate the Sum and Average of Elements in an ArrayList in Java M mrstax Follow Improve Article Tags : Java Java Programs Java-Collections Java-ArrayList Practice Tags : JavaJava-Collections Similar Reads How to Declare an ArrayList with Values in Java? ArrayList is simply known as a resizable array. Declaring an ArrayList with values is a basic task that involves the initialization of a list with specific elements. It is said to be dynamic as the size of it can be changed. Proceeding with the declaration of ArrayList, we have to be aware of the co 2 min read How to Add Element in Java ArrayList? Java ArrayList class uses a dynamic array for storing the elements. It is like an array, but there is no size limit. We can add or remove elements anytime. So, it is much more flexible than the traditional array. Element can be added in Java ArrayList using add() method of java.util.ArrayList class. 2 min read Java Program to Find Sum of Array Elements Given an array of integers. Write a Java Program to find the sum of the elements of the array. Examples: Input : arr[] = {1, 2, 3} Output : 6 1 + 2 + 3 = 6 Input : arr[] = {15, 12, 13, 10} Output : 50 15 + 12 + 13 + 10 = 50 An array is a data structure that contains a group of elements. Typically th 3 min read How Objects Can an ArrayList Hold in Java? ArrayList is a part of the collection framework and is present in java.util package. It provides us with dynamic arrays in Java just as Vector in C++. Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed. In order to understan 3 min read How to Convert an ArrayList Containing Integers to Primitive Int Array? In Java, ArrayList is the pre-defined class of the Java Collection Framework. It is part of the java.util package. ArrayList can be used to add or remove an element dynamically in the Java program. It can be snipped dynamically based on the elements added or removed into the ArrayList. In this artic 2 min read Like