Convert php array_sum to java
$a = array(2,开发者_StackOverflow 4, 6, 8);
echo "sum(a) = " . array_sum($a) . "\n"; //==20
how i can do this in java?
long array_sum(int...array)
{
long sum = 0;
for(int value : array)
sum += value;
return sum;
}
Example:
System.out.println(array_sum(1, 2, 3, -1));
System.out.println(array_sum(1, 2));
System.out.println(array_sum(new int[]{1,2,3,4,5}));
5
3
15
Google search says, "No". Is it not possible to loop through the array and find the sum?
精彩评论