Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform a sum of an int[] array [duplicate]

Tags:

java

arrays

loops

Given an array A of 10 ints, initialize a local variable called sum and use a loop to find the sum of all numbers in the array A.

This was my answer that I submitted:

sum = 0;
while( A, < 10) {
   sum = sum += A;
}

I didn't get any points on this question. What did I do wrong?

like image 255
Jordan Westlund Avatar asked Nov 26 '22 20:11

Jordan Westlund


1 Answers

Once java-8 is out (March 2014) you'll be able to use streams:

int sum = IntStream.of(a).sum();

or even

int sum = IntStream.of(a).parallel().sum();
like image 85
msayag Avatar answered Nov 29 '22 10:11

msayag