Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Calculate the Sum and Average of Elements in an ArrayList in Java
Next article icon

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 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 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 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 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.

Next Article
Calculate the Sum and Average of Elements in an ArrayList in Java

M

mrstax
Improve
Article Tags :
  • Java
  • Java Programs
  • Java-Collections
  • Java-ArrayList
Practice Tags :
  • Java
  • Java-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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences