Java Arena
Step 5 of 7

Lists as parameters

A list can be a method's parameter, like any other value. The parameter's type is the list's whole type, such as ArrayList<String>:

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        ArrayList<String> guests = new ArrayList<>();
        guests.add("Mira");
        guests.add("Oskar");
        printInvitations(guests);

        ArrayList<Integer> scores = new ArrayList<>();
        scores.add(34);
        scores.add(52);
        scores.add(41);
        System.out.println("Best score: " + highest(scores));
    }

    public static void printInvitations(ArrayList<String> names) {
        for (String name : names) {
            System.out.println("Dear " + name + ", you're invited!");
        }
    }

    public static int highest(ArrayList<Integer> numbers) {
        int best = numbers.get(0);
        for (int number : numbers) {
            if (number > best) {
                best = number;
            }
        }
        return best;
    }
}
Output
Dear Mira, you're invited!
Dear Oskar, you're invited!
Best score: 52
  • The one import java.util.ArrayList; at the top of the file covers every method in it.
  • The call gives the list by its name: printInvitations(guests). As with any parameter, the names in the call and in the method don't have to match.
  • A method can return something worked out from the list: a sum, a count, the largest value. (highest expects at least one value: on an empty list, numbers.get(0) crashes.)

A method can also build a new list and return it. Its return type is then the list's type:

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        ArrayList<Integer> scores = new ArrayList<>();
        scores.add(7);
        scores.add(12);
        scores.add(9);
        System.out.println(withBonus(scores, 5));
        System.out.println(scores);
    }

    public static ArrayList<Integer> withBonus(ArrayList<Integer> scores, int bonus) {
        ArrayList<Integer> result = new ArrayList<>();
        for (int score : scores) {
            result.add(score + bonus);
        }
        return result;
    }
}
Output
[12, 17, 14]
[7, 12, 9]

The original list is left as it was: the method only read it, and put the new values in a list of its own.

Challenges that pass a list

The checks in this step build a list, fill it, and call your method with it, just as main does above.

Your task

Challenge 1 of 3

Write a method groupPrice(ArrayList<Integer> ages) that returns what a group pays for museum tickets: 12 for each person aged 12 or over, 5 for each child from 3 to 11, and nothing for a child under 3. The check builds a list of the ages 34, 8, 2 and 40, and prints what groupPrice returns:

29

Challenge 2

Write a method printNumbered(ArrayList<String> items) that prints the items one per line, each with its number in front: 1. for the first, 2. for the second, and so on. The check builds a list of oats, pears and honey, and calls printNumbered(items);:

1. oats
2. pears
3. honey

Challenge 3

Write a method onlyEven(ArrayList<Integer> numbers) that returns a new list with just the even values of numbers, in the same order. The original list stays as it was. The check builds a list of 4, 7, 12 and 9, and prints what onlyEven returns:

[4, 12]