About 16,200 results
Open links in new tab
  1. How do you find the sum of all the numbers in an array in Java?

    Dec 29, 2010 · If you're using Java 8, the Arrays class provides a stream(int[] array) method which returns a sequential IntStream with the specified int array. It has also been overloaded for double and long arrays. int [] arr = {1,2,3,4}; int sum = Arrays.stream(arr).sum(); //prints 10

  2. Java Program to Find Sum of Array Elements - GeeksforGeeks

    Jan 26, 2023 · 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 …

  3. Java How To Calculate the Sum of Array Elements - W3Schools

    Get the sum of array elements: Example int[] myArray = {1, 5, 10, 25}; int sum = 0; int i; // Loop through the array elements and store the sum in the sum variable for (i = 0; i < myArray.length; i++) { sum += myArray[i]; } System.out.println("The sum is: " + sum);

  4. Summing Numbers with Java Streams - Baeldung

    May 11, 2024 · This method takes a mapper as a parameter, which it uses to do the conversion, then we can call the sum() method to calculate the sum of the stream’s elements. Let’s see a quick example of how we can use it: List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5); Integer sum = integers.stream() .mapToInt(Integer::intValue) .sum();

  5. Get sum of all elements of an array in Java 8 and above

    Jan 14, 2022 · With the introduction of Java 8 Stream, we can easily get a sum of all array elements using the Stream.sum() method. To get a stream of array elements, we can use the Arrays.stream() method. This method is overloaded for arrays of int, double and long type.

  6. Program to find sum of elements in a given array

    Sep 20, 2024 · You are given an array of n-elements and an odd-integer m. You have to construct a new sum_array from given array such that sum_array[i] = ?arr[j] for (i-(m/2)) < j (i+(m/2)). note : for 0 > j or j >= n take arr[j] = 0. Examples: Input : arr[] = {1, 2, 3, 4, 5}, m = 3 Output : sum_array =

  7. Java Sum: Array, Map and List Collection - ConcretePage.com

    Dec 13, 2023 · On this page, we will provide Java 8 sum of values of Array, Map and List collection example using reduce() and collect() methods. There are various ways to calculate the sum of values in Java 8. We can use IntStream.sum(), summary statistics and can also create our own method to get the sum.

  8. Find Sum and Average in a Java Array - Baeldung

    Aug 16, 2024 · In this quick tutorial, we’ll cover how to calculate the sum and average of the elements in an array using both Java standard loops and the Stream API. For simplicity, we’ll ignore the cases where the input array is null or empty.

  9. Sum of an array in java in 5 ways with examples - codippa

    Mar 25, 2020 · Learn different methods to calculate the sum of elements of array in java. This post uses for loop, streams and Apache Match library to sum array elements.

  10. Java Program to print the sum of all the items of the array

    Mar 17, 2025 · In this program, we need to calculate the sum of all the elements of an array. This can be solved by looping through the array and add the value of the element in each iteration to variable sum. Sum of all elements of an array is 1 + 2 + 3 + 4 + 5 = 15.

  11. Some results have been removed
Refresh