To get the sum of array's element we have two methods....
First one is traditional method in which we iterate the array's element one by one and add them.
Second one is the using Arrays.stream() method that has one method called sum().
lets have look how these concept will implemented.
Method #1
class arraySum
{
public static void
main(String[] args)
{
int arr[]=new int[]{1,2,3,4,5,6,7,8,9};
int sum=0;
for(int i=0;i<arr.length;i++)
{
sum+=arr[i];
}
System.out.println("Sum of array's Elements ::"+sum);
}
}
Method #2
import java.util.*;
class arraySum
{
public static void
main(String[] args)
{
int arr[]=new int[]{1,2,3,4,5,6,7,8,9};
System.out.println("Sum of array's Elements :: " +Arrays.stream(arr).sum());
}
}
Output: