[Java] How to compute sum of all elements from an array?
I want to find the sum of all elements from an array, how can I do that in Java?
1 Answer
3 years ago by Eleven
We can use the streams to do this. Folling code shows how to do that
import java.util.Arrays;
public class JavaArrays {
public static void main(String[] args) {
int[] numbers = {45, 22, 67, 90 };
System.out.println("Sum of all elements is: " + Arrays.stream(numbers).sum());
}
}
Output:
Sum of all elements is: 224
Try your self here: https://onecompiler.com/java/3xmstms2t
3 years ago by Karthik Divi