Java Program to Compute the Sum of Numbers in a List Using For-Loop


Introduction

Java is a popular programming language that is used for developing a wide range of applications, including those that involve working with lists of numbers. One common task is to compute the sum of numbers in a list, which can be accomplished using a for-loop. In this approach, we first create a list of numbers, then initialize a variable to hold the sum. We then use a for-loop to iterate over each element in the list, adding it to the sum variable. After the loop has completed, the sum variable contains the total sum of all the numbers in the list.

This approach is simple, efficient, and easy to understand, making it a good choice for many applications. However, as the size of the list grows, the time complexity of this approach can become a concern, and other methods such as parallel processing or using streams may be more appropriate.

Example 1

This approach uses a simple for-loop to iterate through the list and add up the elements. It is a straightforward and efficient approach that works well for small to medium-sized lists. For larger lists, other approaches such as parallel processing or using streams might be more efficient.

Approach

  • Create an empty list of integers using the ArrayList class.

  • Add some numbers to the list using the add method.

  • Initialize an integer variable called sum to 0.

  • Use a for-loop to iterate through the list using the size method to determine the number of iterations needed.

  • In each iteration of the loop, get the current element from the list using the get method and add it to the sum variable.

  • After the loop completes, the sum variable will contain the sum of all the numbers in the list.

  • Print out the sum using the println method of the System.out object.

Here's an example Java program that computes the sum of numbers in a list using a for-loop −

import java.util.ArrayList;
import java.util.List;

public class SumOfNumbers {
   public static void main(String[] args) {
      List<Integer> numbers = new ArrayList<>(); // create a list of integers

      // add some numbers to the list
      numbers.add(10);
      numbers.add(20);
      numbers.add(30);
      numbers.add(40);
      numbers.add(50);

      int sum = 0;
      for (int i = 0; i < numbers.size(); i++) { // loop through the list
         sum += numbers.get(i); // add the current element to the sum
      }

      System.out.println("Sum of numbers in the list: " + sum); // print the sum
   }
}

Explanation

In this program, we first create a list of integers using the ArrayList class. We add some numbers to the list using the add method.

Next, we define an integer variable called sum and initialize it to 0. We then use a for-loop to loop through the list. In each iteration of the loop, we get the current element using the get method and add it to the sum variable.

Finally, we print out the sum using the println method of the System.out object.

Output

Sum of numbers in the list: 150

Example 2

This approach is similar to using a for-loop, but it uses a for-each loop instead. A for-each loop is a shorthand version of a for-loop that allows us to iterate through the elements of a collection or array without having to use an index variable.

Approach

  • Create an empty list of integers using the ArrayList class.

  • Add some numbers to the list using the add method.

  • Initialize an integer variable called sum to 0.

  • Use a for-each loop to iterate through the list.

  • In each iteration of the loop, get the current element from the list using the loop variable number and add it to the sum variable.

  • After the loop completes, the sum variable will contain the sum of all the numbers in the list.

  • Print out the sum using the println method of the System.out object.

Here's another example Java program to compute the sum of numbers in a list using a for-each loop −

import java.util.ArrayList;
import java.util.List;

public class SumOfNumbers {
   public static void main(String[] args) {
      List<Integer> numbers = new ArrayList<>(); // create a list of integers

      // add some numbers to the list
      numbers.add(5);
      numbers.add(15);
      numbers.add(25);
      numbers.add(35);
      numbers.add(45);

      int sum = 0;
      for (int number : numbers) { // loop through the list using a for-each loop
         sum += number; // add the current element to the sum
      }

      System.out.println("Sum of numbers in the list: " + sum); // print the sum
   }
}

Explanation

In this program, we create a list of integers using the ArrayList class and add some numbers to the list using the add method.

Next, we define an integer variable called sum and initialize it to 0. We then use a for-each loop to loop through the list. In each iteration of the loop, we get the current element using the loop variable number and add it to the sum variable.

Finally, we print out the sum using the println method of the System.out object.

Output

Sum of numbers in the list: 125

Conclusion

  • There are multiple ways to compute the sum of numbers in a list using Java. Two common approaches are using a for-loop and using a for-each loop. Both approaches have a time complexity of O(n), where n is the number of elements in the list, and a space complexity of O(1) for the for-loop and O(n) for the for-each loop.

  • When choosing which approach to use, it's important to consider the size of the list, the performance requirements of the application, and the readability and maintainability of the code. For small to medium-sized lists, either approach is fine, but for larger lists or performance-critical applications, other approaches such as parallel processing or using streams might be more suitable.

Updated on: 10-Apr-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements