Calculating the average of array elements is a common programming task, especially when working with numerical data. This tutorial by The Coding College demonstrates how to find the average of array elements in Java efficiently.
What Is the Average?
The average is calculated by dividing the sum of all array elements by the total number of elements in the array.
Formula:

Example: Find the Average of an Array of Numbers
Here’s how to calculate the average of an array using a simple loop.
Example Code
public class Main {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
int sum = 0;
// Calculate the sum of the array elements
for (int num : numbers) {
sum += num;
}
// Calculate the average
double average = (double) sum / numbers.length;
// Print the result
System.out.println("Sum of elements: " + sum);
System.out.println("Average: " + average);
}
}
Output:
Sum of elements: 150
Average: 30.0
Example: Using Streams for Average
Java 8 introduced the Stream
API, which provides a concise way to calculate the average.
Example Code
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
// Calculate the average using streams
double average = Arrays.stream(numbers).average().orElse(0);
// Print the result
System.out.println("Average: " + average);
}
}
Output:
Average: 30.0
Example: Average of Array Elements with User Input
In this example, the array elements are provided by the user.
Example Code
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of elements: ");
int n = scanner.nextInt();
int[] numbers = new int[n];
int sum = 0;
System.out.println("Enter the elements:");
for (int i = 0; i < n; i++) {
numbers[i] = scanner.nextInt();
sum += numbers[i];
}
double average = (double) sum / n;
System.out.println("Sum of elements: " + sum);
System.out.println("Average: " + average);
scanner.close();
}
}
Output:
Enter the number of elements: 5
Enter the elements:
10
20
30
40
50
Sum of elements: 150
Average: 30.0
Example: Average of Floating-Point Numbers
You can also calculate the average for floating-point numbers.
Example Code
public class Main {
public static void main(String[] args) {
double[] numbers = {12.5, 15.3, 18.7, 22.1, 10.0};
double sum = 0;
for (double num : numbers) {
sum += num;
}
double average = sum / numbers.length;
System.out.println("Sum of elements: " + sum);
System.out.println("Average: " + average);
}
}
Output:
Sum of elements: 78.6
Average: 15.72
Best Practices
- Validate Input: Ensure the array is not empty to avoid division by zero.
- Use Streams for Simplicity: The
Stream
API is ideal for concise code. - Choose the Right Data Type: Use
double
for floating-point precision if the average can result in decimals.
Conclusion
Finding the average of array elements in Java is straightforward and can be achieved using loops, streams, or user input methods. Whether you’re a beginner or an experienced developer, mastering this concept is essential for data analysis and other programming tasks.
For more coding tutorials, visit The Coding College and enhance your programming skills!