{"id":"lists-types","slug":"what-a-list-holds","title":"What a list holds","text":"The type between `<` and `>` says what goes into the list. A list of whole numbers is an `ArrayList<Integer>`, a list of decimal numbers an `ArrayList<Double>`, and a list of true and false values an `ArrayList<Boolean>`:\n\n```java\nArrayList<Integer> scores = new ArrayList<>();\nscores.add(12);\nscores.add(7);\nscores.add(12);\nSystem.out.println(scores);\n\nArrayList<Double> prices = new ArrayList<>();\nprices.add(4.5);\nprices.add(3.0);\nSystem.out.println(prices);\n```\n\n```output\n[12, 7, 12]\n[4.5, 3.0]\n```\n\nOnly values of that type fit: `scores.add(\"twelve\")` doesn't compile, because `\"twelve\"` is a String, not a whole number.\n\n### Integer, not int\n\nWhy `Integer` and not `int`? A list can only hold **objects**, and `int`, `double` and `boolean` aren't objects: they're **primitive** types, plain values built into the language. Each of them has an object partner, written with a capital letter: `Integer` for `int`, `Double` for `double` and `Boolean` for `boolean`. A primitive type in a list doesn't compile:\n\n```java\nimport java.util.ArrayList;\n\npublic class Main {\n    public static void main(String[] args) {\n        ArrayList<int> scores = new ArrayList<>();\n        scores.add(12);\n        System.out.println(scores);\n    }\n}\n```\n\n```javac\nMain.java:5: error: unexpected type\n        ArrayList<int> scores = new ArrayList<>();\n                  ^\n  required: reference\n  found:    int\n1 error\n```\n\n`required: reference` means javac wants an object type there. Write `ArrayList<Integer>` instead.\n\nOtherwise you rarely notice the difference, because Java converts between the two by itself. `scores.add(12)` turns the `int` 12 into an `Integer`, and a value you take out of the list can go straight into an `int` variable (the next step shows how).\n\nOne catch: Java turns an `int` into an `Integer`, but never into a `Double`. In an `ArrayList<Double>`, write whole numbers with a decimal point: `prices.add(3.0)` works, `prices.add(3)` doesn't compile.\n\nStrings are objects already, so a list of text is simply an `ArrayList<String>`.\n","task":"Fill in the two list types so the program compiles and prints:\n\n```\n[14, 3]\n[true, false]\n```\n","kind":"fill","seed":"import java.util.ArrayList;\n\npublic class Main {\n    public static void main(String[] args) {\n        ArrayList<[[Integer]]> seats = new ArrayList<>();\n        seats.add(14);\n        seats.add(3);\n        ArrayList<[[Boolean]]> booked = new ArrayList<>();\n        booked.add(true);\n        booked.add(false);\n        System.out.println(seats);\n        System.out.println(booked);\n    }\n}\n","solution":"import java.util.ArrayList;\n\npublic class Main {\n    public static void main(String[] args) {\n        ArrayList<Integer> seats = new ArrayList<>();\n        seats.add(14);\n        seats.add(3);\n        ArrayList<Boolean> booked = new ArrayList<>();\n        booked.add(true);\n        booked.add(false);\n        System.out.println(seats);\n        System.out.println(booked);\n    }\n}\n","hints":["The types in a list are object types, written with a capital letter.","`14` and `3` are whole numbers, so the first list is an `ArrayList<Integer>`. `true` and `false` go in an `ArrayList<Boolean>`."],"tests":[{"name":"Output","stdin":"","expect":"[14, 3]\n[true, false]","hidden":false}],"require":[],"forbid":[],"more":[{"task":"Read how many days of rain were measured, and then each day's rainfall in millimeters, as a decimal number, one per line. Add each day's rainfall to an `ArrayList<Double>`, and print `Rainfall: ` followed by the list. For the input `3`, `2.5`, `4` and `0.75` the program prints:\n\n```\nRainfall: [2.5, 4.0, 0.75]\n```\n","kind":"code","seed":"import java.util.ArrayList;\nimport java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        int days = Integer.valueOf(scanner.nextLine());\n\n    }\n}\n","solution":"import java.util.ArrayList;\nimport java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        int days = Integer.valueOf(scanner.nextLine());\n        ArrayList<Double> rainfall = new ArrayList<>();\n        for (int i = 0; i < days; i++) {\n            double millimeters = Double.valueOf(scanner.nextLine());\n            rainfall.add(millimeters);\n        }\n        System.out.println(\"Rainfall: \" + rainfall);\n    }\n}\n","hints":["Create the list with `ArrayList<Double> rainfall = new ArrayList<>();`, then read the values in a `for` loop that runs `days` times.","Read each value with `Double.valueOf(scanner.nextLine())` and add it to the list. A `4` read this way becomes `4.0`."],"tests":[{"name":"Test 1","stdin":"3\n2.5\n4\n0.75\n","expect":"Rainfall: [2.5, 4.0, 0.75]","hidden":false},{"name":"Test 2","stdin":"1\n12\n","expect":"Rainfall: [12.0]","hidden":true},{"name":"Test 3","stdin":"4\n0\n1.5\n3.25\n10\n","expect":"Rainfall: [0.0, 1.5, 3.25, 10.0]","hidden":true},{"name":"Test 4","stdin":"0\n","expect":"Rainfall: []","hidden":true}],"require":[{"pattern":"ArrayList\\s*<\\s*Double\\s*>","message":"Keep the rainfall in an `ArrayList<Double>`."},{"pattern":"\\.add\\s*\\(","message":"Add each value to the list with `add`."}],"forbid":[]},{"task":"This program doesn't compile: javac finds two errors, one for each list. Fix them so the program prints:\n\n```\n[31, 8]\n[1.82, 1.0]\n```\n","kind":"code","seed":"import java.util.ArrayList;\n\npublic class Main {\n    public static void main(String[] args) {\n        ArrayList<int> ages = new ArrayList<>();\n        ages.add(31);\n        ages.add(8);\n        ArrayList<Double> heights = new ArrayList<>();\n        heights.add(1.82);\n        heights.add(1);\n        System.out.println(ages);\n        System.out.println(heights);\n    }\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(31);\n        ages.add(8);\n        ArrayList<Double> heights = new ArrayList<>();\n        heights.add(1.82);\n        heights.add(1.0);\n        System.out.println(ages);\n        System.out.println(heights);\n    }\n}\n","hints":["A list holds objects: the type for whole numbers is `Integer`, not `int`.","Java won't turn the `int` 1 into a `Double`. Write it as a decimal number: `heights.add(1.0);`"],"tests":[{"name":"The program","stdin":"","expect":"[31, 8]\n[1.82, 1.0]","hidden":false}],"require":[{"pattern":"ArrayList\\s*<\\s*Integer\\s*>","message":"Make `ages` an `ArrayList<Integer>`."}],"forbid":[{"pattern":"\"\\s*\\[","raw":true,"message":"Print the lists themselves, not text that looks like them."}]}]}