{"id":"lists-size","slug":"size","title":"The size of a list","text":"`size()` tells how many values a list holds right now. A new list has size 0, and every `add` makes it one bigger:\n\n```java\nArrayList<String> guests = new ArrayList<>();\nSystem.out.println(guests.size());\nguests.add(\"Rosa\");\nguests.add(\"Tariq\");\nguests.add(\"Yuki\");\nSystem.out.println(guests.size());\nSystem.out.println(\"Last to arrive: \" + guests.get(guests.size() - 1));\n```\n\n```output\n0\n3\nLast to arrive: Yuki\n```\n\nSince indexes start at 0, the last index is always `size() - 1`. So `guests.get(guests.size() - 1)` is the last value, however long the list is. (On an empty list, that's `get(-1)`, which crashes: check that the size is above 0 first.)\n\n### Reading an unknown number of values\n\n`size()` is what makes lists so handy for input: the program reads as many values as it's given, and counts them afterwards.\n\n```java\nimport 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> trees = new ArrayList<>();\n        while (true) {\n            String tree = scanner.nextLine();\n            if (tree.equals(\"end\")) {\n                break;\n            }\n            trees.add(tree);\n        }\n        System.out.println(\"Trees seen: \" + trees.size());\n        if (trees.size() > 0) {\n            System.out.println(\"First: \" + trees.get(0));\n            System.out.println(\"Last: \" + trees.get(trees.size() - 1));\n        }\n    }\n}\n```\n\n```input\nmaple\noak\nbirch\nend\n```\n\n```output\nTrees seen: 3\nFirst: maple\nLast: birch\n```\n\n### Checking an index\n\nWith `size()`, a program can check an index before it uses it. An index is valid when it's at least 0 and less than the size:\n\n```java\nif (index >= 0 && index < trees.size()) {\n    System.out.println(trees.get(index));\n} else {\n    System.out.println(\"No tree at index \" + index);\n}\n```\n","task":"Fill in the blanks so the program prints how many photos there are, and which one was added last:\n\n```\nPhotos: 4\nNewest: pier.jpg\n```\n","kind":"fill","seed":"import java.util.ArrayList;\n\npublic class Main {\n    public static void main(String[] args) {\n        ArrayList<String> photos = new ArrayList<>();\n        photos.add(\"beach.jpg\");\n        photos.add(\"dunes.jpg\");\n        photos.add(\"sunset.jpg\");\n        photos.add(\"pier.jpg\");\n        System.out.println(\"Photos: \" + photos.[[size()]]);\n        System.out.println(\"Newest: \" + photos.get([[photos.size() - 1‖(photos.size() - 1)]]));\n    }\n}\n","solution":"import java.util.ArrayList;\n\npublic class Main {\n    public static void main(String[] args) {\n        ArrayList<String> photos = new ArrayList<>();\n        photos.add(\"beach.jpg\");\n        photos.add(\"dunes.jpg\");\n        photos.add(\"sunset.jpg\");\n        photos.add(\"pier.jpg\");\n        System.out.println(\"Photos: \" + photos.size());\n        System.out.println(\"Newest: \" + photos.get(photos.size() - 1));\n    }\n}\n","hints":["`photos.size()` is the number of values in the list.","The last index is one less than the size: `photos.size() - 1`."],"tests":[{"name":"Output","stdin":"","expect":"Photos: 4\nNewest: pier.jpg","hidden":false}],"require":[{"pattern":"\\bphotos\\.size\\s*\\(\\s*\\)","min":2,"message":"Use `photos.size()` in both lines, so the program works for any number of photos."}],"forbid":[],"more":[{"task":"The block party is held in front of the house in the middle of the block. Read the house numbers along the block, in order, one per line, until `0`. Keep them in a list. Then print `Houses: ` and how many there are, and on the next line `Party at number ` and the number of the middle house. With an even number of houses, take the later of the two in the middle. When no houses were read, print `Houses: 0` and then `No party`. For the input `101`, `103`, `105`, `107`, `109` and `0` the program prints:\n\n```\nHouses: 5\nParty at number 105\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<Integer> houses = 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<Integer> houses = new ArrayList<>();\n        while (true) {\n            int number = Integer.valueOf(scanner.nextLine());\n            if (number == 0) {\n                break;\n            }\n            houses.add(number);\n        }\n        System.out.println(\"Houses: \" + houses.size());\n        if (houses.size() > 0) {\n            System.out.println(\"Party at number \" + houses.get(houses.size() / 2));\n        } else {\n            System.out.println(\"No party\");\n        }\n    }\n}\n","hints":["After the loop, `houses.size()` is the number of houses. Check that it's above 0 before you use `get`.","The middle index is `houses.size() / 2`. It's whole-number division: for 5 houses it's 2 (the third house), and for 4 houses it's also 2, the later of the two middle ones."],"tests":[{"name":"Test 1","stdin":"101\n103\n105\n107\n109\n0\n","expect":"Houses: 5\nParty at number 105","hidden":false},{"name":"Test 2","stdin":"12\n14\n16\n18\n0\n","expect":"Houses: 4\nParty at number 16","hidden":true},{"name":"Test 3","stdin":"7\n0\n","expect":"Houses: 1\nParty at number 7","hidden":true},{"name":"Test 4","stdin":"21\n22\n0\n","expect":"Houses: 2\nParty at number 22","hidden":true},{"name":"Test 5","stdin":"0\n","expect":"Houses: 0\nNo party","hidden":true}],"require":[{"pattern":"\\bhouses\\.size\\s*\\(\\s*\\)","min":2,"message":"Use `houses.size()` for the number of houses, and to find the middle one."}],"forbid":[{"pattern":"\\+\\+|\\+=","message":"Let the list do the counting: use `size()` instead of a counter of your own."}]},{"task":"A coat check hands out numbered tickets: the first coat gets ticket 1, the second ticket 2, and so on. Read the coats handed in, one per line, until `end`. Then read a ticket number, and print the coat for that ticket. When there's no coat for the ticket (0, a negative number, or a number past the last coat), print `No coat for ticket` and the number. For the input `green scarf`, `black coat`, `red hat`, `end` and `3` the program prints:\n\n```\nTicket 3: red hat\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> coats = 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> coats = new ArrayList<>();\n        while (true) {\n            String coat = scanner.nextLine();\n            if (coat.equals(\"end\")) {\n                break;\n            }\n            coats.add(coat);\n        }\n        int ticket = Integer.valueOf(scanner.nextLine());\n        if (ticket >= 1 && ticket <= coats.size()) {\n            System.out.println(\"Ticket \" + ticket + \": \" + coats.get(ticket - 1));\n        } else {\n            System.out.println(\"No coat for ticket \" + ticket);\n        }\n    }\n}\n","hints":["Ticket 1 is index 0, so the coat for a ticket is `coats.get(ticket - 1)`.","A ticket is valid when `ticket >= 1 && ticket <= coats.size()`."],"tests":[{"name":"Test 1","stdin":"green scarf\nblack coat\nred hat\nend\n3\n","expect":"Ticket 3: red hat","hidden":false},{"name":"Test 2","stdin":"green scarf\nblack coat\nred hat\nend\n4\n","expect":"No coat for ticket 4","hidden":true},{"name":"Test 3","stdin":"green scarf\nblack coat\nend\n1\n","expect":"Ticket 1: green scarf","hidden":true},{"name":"Test 4","stdin":"umbrella\nend\n0\n","expect":"No coat for ticket 0","hidden":true},{"name":"Test 5","stdin":"end\n1\n","expect":"No coat for ticket 1","hidden":true},{"name":"Test 6","stdin":"umbrella\nend\n-2\n","expect":"No coat for ticket -2","hidden":true}],"require":[{"pattern":"\\bcoats\\.size\\s*\\(\\s*\\)","message":"Check the ticket against `coats.size()`, so the program works for any number of coats."}],"forbid":[]}]}