Menu
Coddy logo textTech

Common Array Operations

Part of the Fundamentals section of Coddy's Java journey — lesson 66 of 73.

Here are some common array operations:

  • Find the sum of all elements in an array:
int[] numbers = {1, 2, 3, 4, 5};
int sum = 0;
for (int number : numbers) {
    sum += number;
}
System.out.println("Sum: " + sum);
  • Find the average of elements in an array:
int[] numbers = {1, 2, 3, 4, 5};
int sum = 0;
for (int number : numbers) {
    sum += number;
}
double average = (double) sum / numbers.length;
System.out.println("Average: " + average);
  • Find the maximum element in an array:
int[] numbers = {1, 5, 2, 9, 3};
int max = numbers[0];
for (int i = 1; i < numbers.length; i++) {
    if (numbers[i] > max) {
        max = numbers[i];
    }
}
System.out.println("Max: " + max);
  • Find the minimum element in an array:
int[] numbers = {1, 5, 2, 9, 3};
int min = numbers[0];
for (int i = 1; i < numbers.length; i++) {
    if (numbers[i] < min) {
        min = numbers[i];
    }
}
System.out.println("Min: " + min);
challenge icon

Challenge

Easy

Create a method named calculateStats that takes an array of integers as input and performs the following operations:

  1. Calculates the sum of all elements in the array.
  2. Calculates the average of the elements in the array.
  3. Finds the maximum element in the array.
  4. Finds the minimum element in the array.

The method should return an array of doubles containing the sum, average, maximum, and minimum, in that order.

Cheat sheet

Common array operations in Java:

Sum of all elements:

int[] numbers = {1, 2, 3, 4, 5};
int sum = 0;
for (int number : numbers) {
    sum += number;
}

Average of elements:

int[] numbers = {1, 2, 3, 4, 5};
int sum = 0;
for (int number : numbers) {
    sum += number;
}
double average = (double) sum / numbers.length;

Maximum element:

int[] numbers = {1, 5, 2, 9, 3};
int max = numbers[0];
for (int i = 1; i < numbers.length; i++) {
    if (numbers[i] > max) {
        max = numbers[i];
    }
}

Minimum element:

int[] numbers = {1, 5, 2, 9, 3};
int min = numbers[0];
for (int i = 1; i < numbers.length; i++) {
    if (numbers[i] < min) {
        min = numbers[i];
    }
}

Try it yourself

import java.util.Scanner;

public class Main {
    public static double[] calculateStats(int[] arr) {
        // Write your code here
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String text = scanner.nextLine();
        String[] arrString = text.split(",");
        int[] numbers = new int[arrString.length];
        for (int i = 0; i < arrString.length; i++) {
           numbers[i] = Integer.parseInt(arrString[i]);
        }
        double[] stats = calculateStats(numbers);
        System.out.println("Sum: " + stats[0]);
        System.out.println("Average: " + stats[1]);
        System.out.println("Maximum: " + stats[2]);
        System.out.println("Minimum: " + stats[3]);
    }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals