{"id":"list-methods-parameters","slug":"lists-as-parameters","title":"Lists as parameters","text":"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>`:\n\n```java\nimport java.util.ArrayList;\n\npublic class Main {\n    public static void main(String[] args) {\n        ArrayList<String> guests = new ArrayList<>();\n        guests.add(\"Mira\");\n        guests.add(\"Oskar\");\n        printInvitations(guests);\n\n        ArrayList<Integer> scores = new ArrayList<>();\n        scores.add(34);\n        scores.add(52);\n        scores.add(41);\n        System.out.println(\"Best score: \" + highest(scores));\n    }\n\n    public static void printInvitations(ArrayList<String> names) {\n        for (String name : names) {\n            System.out.println(\"Dear \" + name + \", you're invited!\");\n        }\n    }\n\n    public static int highest(ArrayList<Integer> numbers) {\n        int best = numbers.get(0);\n        for (int number : numbers) {\n            if (number > best) {\n                best = number;\n            }\n        }\n        return best;\n    }\n}\n```\n\n```output\nDear Mira, you're invited!\nDear Oskar, you're invited!\nBest score: 52\n```\n\n- The one `import java.util.ArrayList;` at the top of the file covers every method in it.\n- 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.\n- 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.)\n\nA method can also build a new list and return it. Its return type is then the list's type:\n\n```java\nimport java.util.ArrayList;\n\npublic class Main {\n    public static void main(String[] args) {\n        ArrayList<Integer> scores = new ArrayList<>();\n        scores.add(7);\n        scores.add(12);\n        scores.add(9);\n        System.out.println(withBonus(scores, 5));\n        System.out.println(scores);\n    }\n\n    public static ArrayList<Integer> withBonus(ArrayList<Integer> scores, int bonus) {\n        ArrayList<Integer> result = new ArrayList<>();\n        for (int score : scores) {\n            result.add(score + bonus);\n        }\n        return result;\n    }\n}\n```\n\n```output\n[12, 17, 14]\n[7, 12, 9]\n```\n\nThe original list is left as it was: the method only read it, and put the new values in a list of its own.\n\n### Challenges that pass a list\n\nThe checks in this step build a list, fill it, and call your method with it, just as `main` does above.\n","task":"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:\n\n```\n29\n```\n","kind":"code","seed":"import java.util.ArrayList;\n\npublic class Main {\n    public static void main(String[] args) {\n        ArrayList<Integer> ages = new ArrayList<>();\n        ages.add(34);\n        ages.add(8);\n        ages.add(2);\n        ages.add(40);\n        System.out.println(groupPrice(ages));\n    }\n\n    // Write the groupPrice method here.\n}\n","solution":"import java.util.ArrayList;\n\npublic class Main {\n    public static void main(String[] args) {\n        ArrayList<Integer> ages = new ArrayList<>();\n        ages.add(34);\n        ages.add(8);\n        ages.add(2);\n        ages.add(40);\n        System.out.println(groupPrice(ages));\n    }\n\n    public static int groupPrice(ArrayList<Integer> ages) {\n        int price = 0;\n        for (int age : ages) {\n            if (age >= 12) {\n                price += 12;\n            } else if (age >= 3) {\n                price += 5;\n            }\n        }\n        return price;\n    }\n}\n","hints":["The method starts `public static int groupPrice(ArrayList<Integer> ages) {`.","Start a total at 0. In a for-each loop over the ages, add 12 when `age >= 12`, else 5 when `age >= 3`, and nothing otherwise. Return the total after the loop."],"tests":[{"name":"Test 1","stdin":"","call":"ArrayList<Integer> ages = new ArrayList<>();\nages.add(34);\nages.add(8);\nages.add(2);\nages.add(40);\nSystem.out.println(groupPrice(ages));\n","expect":"29","hidden":false},{"name":"Test 2","stdin":"","call":"ArrayList<Integer> ages = new ArrayList<>();\nSystem.out.println(groupPrice(ages));\n","expect":"0","hidden":true},{"name":"Test 3","stdin":"","call":"ArrayList<Integer> ages = new ArrayList<>();\nages.add(3);\nages.add(11);\nages.add(12);\nages.add(2);\nSystem.out.println(groupPrice(ages));\n","expect":"22","hidden":true},{"name":"Test 4","stdin":"","call":"ArrayList<Integer> ages = new ArrayList<>();\nages.add(70);\nSystem.out.println(groupPrice(ages));\n","expect":"12","hidden":true}],"require":[],"forbid":[],"more":[{"task":"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);`:\n\n```\n1. oats\n2. pears\n3. honey\n```\n","kind":"code","seed":"import java.util.ArrayList;\n\npublic class Main {\n    public static void main(String[] args) {\n        ArrayList<String> items = new ArrayList<>();\n        items.add(\"oats\");\n        items.add(\"pears\");\n        items.add(\"honey\");\n        printNumbered(items);\n    }\n\n    // Write the printNumbered method here.\n}\n","solution":"import java.util.ArrayList;\n\npublic class Main {\n    public static void main(String[] args) {\n        ArrayList<String> items = new ArrayList<>();\n        items.add(\"oats\");\n        items.add(\"pears\");\n        items.add(\"honey\");\n        printNumbered(items);\n    }\n\n    public static void printNumbered(ArrayList<String> items) {\n        for (int i = 0; i < items.size(); i++) {\n            System.out.println((i + 1) + \". \" + items.get(i));\n        }\n    }\n}\n","hints":["The method is `void`: `public static void printNumbered(ArrayList<String> items) {`.","The number is the index plus one, so an index loop fits: `(i + 1) + \". \" + items.get(i)`."],"tests":[{"name":"Test 1","stdin":"","call":"ArrayList<String> items = new ArrayList<>();\nitems.add(\"oats\");\nitems.add(\"pears\");\nitems.add(\"honey\");\nprintNumbered(items);\n","expect":"1. oats\n2. pears\n3. honey","hidden":false},{"name":"Test 2","stdin":"","call":"ArrayList<String> items = new ArrayList<>();\nitems.add(\"rice\");\nprintNumbered(items);\n","expect":"1. rice","hidden":true},{"name":"Test 3","stdin":"","call":"ArrayList<String> items = new ArrayList<>();\nitems.add(\"salt\");\nitems.add(\"tea\");\nitems.add(\"jam\");\nitems.add(\"figs\");\nprintNumbered(items);\n","expect":"1. salt\n2. tea\n3. jam\n4. figs","hidden":true}],"require":[],"forbid":[]},{"task":"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:\n\n```\n[4, 12]\n```\n","kind":"code","seed":"import java.util.ArrayList;\n\npublic class Main {\n    public static void main(String[] args) {\n        ArrayList<Integer> numbers = new ArrayList<>();\n        numbers.add(4);\n        numbers.add(7);\n        numbers.add(12);\n        numbers.add(9);\n        System.out.println(onlyEven(numbers));\n    }\n\n    // Write the onlyEven method here.\n}\n","solution":"import java.util.ArrayList;\n\npublic class Main {\n    public static void main(String[] args) {\n        ArrayList<Integer> numbers = new ArrayList<>();\n        numbers.add(4);\n        numbers.add(7);\n        numbers.add(12);\n        numbers.add(9);\n        System.out.println(onlyEven(numbers));\n    }\n\n    public static ArrayList<Integer> onlyEven(ArrayList<Integer> numbers) {\n        ArrayList<Integer> evens = new ArrayList<>();\n        for (int number : numbers) {\n            if (number % 2 == 0) {\n                evens.add(number);\n            }\n        }\n        return evens;\n    }\n}\n","hints":["The return type is a list: `public static ArrayList<Integer> onlyEven(ArrayList<Integer> numbers) {`.","Make a new empty list, add each number where `number % 2 == 0`, and return the new list."],"tests":[{"name":"Test 1","stdin":"","call":"ArrayList<Integer> numbers = new ArrayList<>();\nnumbers.add(4);\nnumbers.add(7);\nnumbers.add(12);\nnumbers.add(9);\nSystem.out.println(onlyEven(numbers));\n","expect":"[4, 12]","hidden":false},{"name":"Test 2","stdin":"","call":"ArrayList<Integer> numbers = new ArrayList<>();\nnumbers.add(-2);\nnumbers.add(-3);\nnumbers.add(0);\nnumbers.add(8);\nSystem.out.println(onlyEven(numbers));\nSystem.out.println(numbers);\n","expect":"[-2, 0, 8]\n[-2, -3, 0, 8]","hidden":true},{"name":"Test 3","stdin":"","call":"ArrayList<Integer> numbers = new ArrayList<>();\nnumbers.add(1);\nnumbers.add(3);\nSystem.out.println(onlyEven(numbers));\nSystem.out.println(numbers);\n","expect":"[]\n[1, 3]","hidden":true}],"require":[],"forbid":[]}]}