How to calculate the average of array using stream in Java

1 Answer

0 votes
import java.util.Arrays; 

public class MyClass {
    public static void main(String args[]) {
        int arr[] = {1, 2, 3, 4, 5, 6};
            
        int sum = Arrays.stream(arr).sum();
        
        System.out.println((float)sum / arr.length); 
    }
}




/*
run:

3.5

*/

 



answered Oct 1, 2019 by avibootz

Related questions

2 answers 281 views
1 answer 179 views
1 answer 192 views
1 answer 276 views
...