-3

How do I sum up all the values in the array and returns the total from a method

double[] dvals = { 88.94, 77.56, -7.0, -203.09 }; // initialize array
double[] dvals2 = new double[8];
System.out.printf("Output of sumAll for dvals : %9.3f\n", sumAll(dvals));
System.out.printf("Output of sumAll for dvals2: %9.3f\n\n", sumAll(dvals2));
Nick
  • 138,499
  • 22
  • 57
  • 95
  • Does this answer your question? [How to sum a list of integers with java streams?](https://stackoverflow.com/questions/30125296/how-to-sum-a-list-of-integers-with-java-streams) – Max Peng Nov 20 '20 at 02:07

1 Answers1

0

There are many good websites and StackOverflow questions to answer this, but I will answer it anyway. You can use a for loop and add to a variable sum at every iteration, or you can simply use Arrays.stream(arr).sum().

null_awe
  • 483
  • 6
  • 17