{"id":"lists-creating","slug":"creating-a-list","title":"Creating a list","text":"Say a program reads the names of everyone coming to a party. With one variable per name, you'd need `name1`, `name2`, `name3` and so on, and you'd have to know in advance how many guests there will be. A **list** solves both problems: it's one variable that holds any number of values, in order.\n\nJava's list is called `ArrayList`:\n\n```java\nimport java.util.ArrayList;\n\npublic class Main {\n    public static void main(String[] args) {\n        ArrayList<String> planets = new ArrayList<>();\n        planets.add(\"Mercury\");\n        planets.add(\"Venus\");\n        planets.add(\"Earth\");\n        System.out.println(planets);\n    }\n}\n```\n\n```output\n[Mercury, Venus, Earth]\n```\n\n- `import java.util.ArrayList;` goes at the very top, like the import for `Scanner`. Without it, Java doesn't know the name `ArrayList`.\n- `ArrayList<String> planets` declares a variable for a list of Strings. The type between `<` and `>` says what kind of values the list holds.\n- `new ArrayList<>()` creates a new, empty list, and `=` stores it in the variable.\n- `planets.add(\"Mercury\")` adds a value at the end of the list. The list grows by one each time; there's no size to set in advance.\n- Printing a list prints all its values in square brackets, separated by commas, in the order they were added. An empty list prints `[]`.\n\nA list can hold the same value more than once. And `+` joins a list to text, just as it does a number: `\"Planets: \" + planets` gives `Planets: [Mercury, Venus, Earth]`.\n","task":"Fill in the blanks so the program creates a list, adds three drinks to it, and prints:\n\n```\n[tea, coffee, cocoa]\n```\n","kind":"fill","seed":"import java.util.[[ArrayList]];\n\npublic class Main {\n    public static void main(String[] args) {\n        ArrayList<String> drinks = [[new ArrayList<>()‖new ArrayList<String>()]];\n        drinks.[[add]](\"tea\");\n        drinks.add(\"coffee\");\n        drinks.add(\"cocoa\");\n        System.out.println(drinks);\n    }\n}\n","solution":"import java.util.ArrayList;\n\npublic class Main {\n    public static void main(String[] args) {\n        ArrayList<String> drinks = new ArrayList<>();\n        drinks.add(\"tea\");\n        drinks.add(\"coffee\");\n        drinks.add(\"cocoa\");\n        System.out.println(drinks);\n    }\n}\n","hints":["The import is `import java.util.ArrayList;`, and `new ArrayList<>()` creates the empty list.","`drinks.add(\"tea\")` puts `tea` at the end of the list."],"tests":[{"name":"Output","stdin":"","expect":"[tea, coffee, cocoa]","hidden":false}],"require":[],"forbid":[],"more":[{"task":"What does this program print? Type each line.\n","kind":"predict","seed":"import java.util.ArrayList;\n\npublic class Main {\n    public static void main(String[] args) {\n        ArrayList<String> queue = new ArrayList<>();\n        System.out.println(queue);\n        queue.add(\"Ana\");\n        queue.add(\"Ben\");\n        System.out.println(queue);\n        queue.add(\"Ana\");\n        System.out.println(\"Queue: \" + queue);\n    }\n}\n","solution":"import java.util.ArrayList;\n\npublic class Main {\n    public static void main(String[] args) {\n        ArrayList<String> queue = new ArrayList<>();\n        System.out.println(queue);\n        queue.add(\"Ana\");\n        queue.add(\"Ben\");\n        System.out.println(queue);\n        queue.add(\"Ana\");\n        System.out.println(\"Queue: \" + queue);\n    }\n}\n","hints":["A new list is empty, and an empty list prints as `[]`.","`add` always puts the value at the end, even when the list already holds it: the last line shows `Ana` twice."],"tests":[{"name":"Output","stdin":"","expect":"[]\n[Ana, Ben]\nQueue: [Ana, Ben, Ana]","hidden":false}],"require":[],"forbid":[],"lines":["[]","[Ana, Ben]","Queue: [Ana, Ben, Ana]"]},{"task":"Read the titles of books you'd like to read, one per line, until the line `stop`. Add each title to the list `books` (but not `stop` itself). After the loop, print `Reading list: ` followed by the list. For the input `Dune`, `Emma`, `Beloved` and `stop` the program prints:\n\n```\nReading list: [Dune, Emma, Beloved]\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        ArrayList<String> books = new ArrayList<>();\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        ArrayList<String> books = new ArrayList<>();\n        while (true) {\n            String title = scanner.nextLine();\n            if (title.equals(\"stop\")) {\n                break;\n            }\n            books.add(title);\n        }\n        System.out.println(\"Reading list: \" + books);\n    }\n}\n","hints":["A `while (true)` loop reads one line per round. When the line is `stop`, `break` ends the loop; otherwise `books.add(title);` adds it.","After the loop, `System.out.println(\"Reading list: \" + books);` prints the whole list."],"tests":[{"name":"Test 1","stdin":"Dune\nEmma\nBeloved\nstop\n","expect":"Reading list: [Dune, Emma, Beloved]","hidden":false},{"name":"Test 2","stdin":"The Hobbit\nstop\n","expect":"Reading list: [The Hobbit]","hidden":true},{"name":"Test 3","stdin":"stop\n","expect":"Reading list: []","hidden":true},{"name":"Test 4","stdin":"Walden\nWalden\nUlysses\nMiddlemarch\nstop\n","expect":"Reading list: [Walden, Walden, Ulysses, Middlemarch]","hidden":true}],"require":[{"pattern":"\\bbooks\\.add\\s*\\(","message":"Add each title to the list with `books.add(...)`."}],"forbid":[]}]}