Home > Java > javaTutorial > body text

Java program to calculate the sum of numbers in a list using a for loop

WBOY
Release: 2023-09-14 19:17:02
forward
1216 people have browsed it

Java program to calculate the sum of numbers in a list using a for loop

Introduction

Java is a popular programming language used to develop a variety of applications, including those involving lists of numbers. A common task is to calculate the sum of numbers in a list, which can be done using a for loop. In this approach, we first create a list of numbers and 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 completes, the sum variable contains the sum of all the numbers in the list.

This method is simple, efficient, and easy to understand, making it a good choice for many applications. However, as the list size grows, the time complexity of this approach may become an issue and other approaches (such as parallel processing or using streams) may be more suitable.

Example 1

This method uses a simple for loop to iterate over the list and add elements. This is a simple and effective method that's perfect for small to medium-sized lists. For larger lists, other methods such as parallel processing or using streams may be more efficient.

method

  • Use the ArrayList class to create an empty list of integers.

  • Use the add method to add some numbers to the list.

  • Initialize the integer variable named sum to 0.

  • Use a for loop to iterate over the list, and use the size method to determine the number of iterations required.

  • On each iteration of the loop, use the get method to get the current element from the list 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.

  • Use the println method of the System.out object to print out the sum.

This is a sample Java program that uses a for loop to calculate the sum of numbers in a list -

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
   }
}
Copy after login

illustrate

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

Next, we define an integer variable named sum and initialize it to 0. Then use a for loop to iterate through the list. On 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
Copy after login

Example 2

This method is similar to using a for loop, but it uses a for-each loop. The for-each loop is a shorthand version of the for-loop that allows us to iterate over the elements of a collection or array without using an index variable.

method

  • Use the ArrayList class to create an empty list of integers.

  • Use the add method to add some numbers to the list.

  • Initialize the integer variable named sum to 0.

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

  • On each iteration of the loop, use the loop variable number to get the current element from the list 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.

  • Use the println method of the System.out object to print out the sum.

This is another example of a Java program that uses a for-each loop to calculate the sum of numbers in a list -

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
   }
}
Copy after login

illustrate

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 named sum and initialize it to 0. We then use a for-each loop to iterate through the list. On 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
Copy after login

in conclusion

  • There are many ways to calculate the sum of numbers in a list using Java. Two common methods are using a for-loop and using a for-each loop. The time complexity of both methods is O(n), where n is the number of elements in the list, the space complexity of the for loop is O(1), and the space complexity of the for-each loop is O(n).

  • When choosing which method to use, it is 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, both approaches are fine, but for larger lists or performance-critical applications, other approaches such as parallel processing or using streams may be more appropriate.

The above is the detailed content of Java program to calculate the sum of numbers in a list using a for loop. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template