# Java Array Tutorials
# Java Array Programs
➤ Find Length of Array
➤ Different ways to Print Array
➤ Sum of Array Elements
➤ Average of Array Elements
➤ Sum of Two Arrays Elements
➤ Compare Two Arrays in Java
➤ 2nd Largest Number in Array
➤ How to Sort an Array in Java
➤ Reverse an Array in Java
➤ GCD of N Numbers in Java
➤ Linear Search in Java
➤ Binary Search in Java
➤ Copy Array in Java
➤ Merge 2 Arrays in Java
➤ Merge two sorted Arrays
➤ Largest Number in Array
➤ Smallest Number in Array
➤ Remove Duplicates
➤ Insert at Specific Position
➤ Add Element to Array
➤ Remove Element From Array
➤ Count Repeated Elements
➤ More Array Programs
Java Matrix Programs
➤ Matrix Tutorial in Java
➤ Print 2D Array in Java
➤ Print a 3×3 Matrix
➤ Sum of Matrix Elements
➤ Sum of Diagonal Elements
➤ Row Sum – Column Sum
➤ Matrix Addition in Java
➤ Matrix Subtraction in Java
➤ Transpose of a Matrix in Java
➤ Matrix Multiplication in Java
➤ Menu-driven Matrix Operations
Program to Find Average in Java Using Array | Array Programs in Java – 6 | In the previous Java program, we find the sum of array elements in Java. Now in this post, we will discuss how to find the average of an array in Java.
Program description:- Write a Java program to calculate the average of numbers or find the average of elements of the given array.
To calculate the average of numbers given in the Java array, first, we need to calculate the sum and then find the average using sum / number_of_array_elements.
Example:-
array[]
= {10, 20, 30, 40, 50};
then sum of array elements = 10 + 20 + 30 + 40 + 50 = 150
and the average of numbers = 150 / 5 = 30
Procedure to develop the Java program to find the average,
1) Take numbers as input and store them into an array
2) Declare a sum variable and initialize it with 0
3) Iterate through the array and add numbers to the sum variable and update the sum variable (i.e. sum = sum + array[i]
)
4) Calculate the average, avg = sum/number_of_array_elements
5) Display the average value
Java program to calculate the average of an array
public class ArrayAverage {
public static void main(String[] args) {
double array[] = {10, 20, 30, 40, 50};
// declare sum variable, & initialize with 0
double sum = 0.0;
// declare average variable
double avg = 0.0;
// loop to iterate the array
for (int i=0; i<array.length; i++) {
// add numbers
sum = sum + array[i];
}
// calculate the average value
avg = sum/array.length;
// display result
System.out.println("Average: " + avg );
}
}
Output:-
Average: 30.0
In this Java program, we have hardcoded the values. Now, let us develop another Java program to calculate the average of an array by taking array elements as input from the end-user. To take input value we can use Scanner class or BufferedReader class, but here we are using the Scanner class. We need to import java.util.Scanner class to use Scanner class methods.
Java program to calculate the average of numbers by taking inputs from end-user
import java.util.Scanner;
public class ArrayAverage {
public static void main(String[] args) {
// create Scanner class object
Scanner scan = new Scanner(System.in);
// take total number of elements
System.out.print("Enter number of elements: ");
int size = scan.nextInt();
// declare an array with given size
double array[] = new double[size];
// declare sum variable, & initialize with 0
double sum = 0.0;
// declare average variable
double avg = 0.0;
// take numbers and store them to the array
System.out.println("Enter array elements: ");
for (int i=0; i<array.length; i++) {
array[i] = scan.nextDouble();
// add numbers
sum = sum + array[i];
}
// calculate the average value
avg = sum/array.length;
// display result
System.out.println("Average: " + avg );
}
}
Output for the different test-cases:-
Enter Number of elements: 5
Enter array elements:
10 20 30 40 50
Average: 30.0
Enter Number of elements: 3
Enter array elements:
19.5 20.9 55.95
Average: 32.11666666666667
Average of Numbers using Stream
When numbers are stored in an array then we can convert the array to an IntStream using the Arrays.stream() method and call the average() method:-
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
double average = Arrays.stream(numbers).average().getAsDouble();
System.out.println(average); // 30.0
}
}
But if the numbers are stored in a list then first we need to convert Stream to IntStream as follows:-
List<Integer> numbers = List.of(10, 20, 30, 40, 50);
double average = numbers.stream()
.mapToInt(Integer::intValue)
.average().getAsDouble();
System.out.println(average); // 30.0
We can also use collect() method:-
List<Integer> numbers = List.of(10, 20, 30, 40, 50);
double average = numbers.stream()
.collect(Collectors.averagingDouble(Integer::intValue));
System.out.println(average); // 30.0
In this program, we have calculated the sum value while taking the input values. You can take a separate loop and add numbers in that specific loop.
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!