{"id":"lists-get","slug":"getting-values","title":"Getting values by index","text":"Every value in a list has an **index**: its position, counting from 0. The first value is at index 0, the second at index 1, and so on. `get(index)` gives back the value at that index:\n\n```java\nArrayList<String> colors = new ArrayList<>();\ncolors.add(\"red\");\ncolors.add(\"green\");\ncolors.add(\"blue\");\nSystem.out.println(colors.get(0));\nSystem.out.println(colors.get(2));\nSystem.out.println(colors.get(1));\n```\n\n```output\nred\nblue\ngreen\n```\n\n| index | 0 | 1 | 2 |\n|---|---|---|---|\n| value | red | green | blue |\n\nSo the third value is at index 2: the index is always one less than the place as people count it. `get` only reads the value, and the list stays as it was.\n\nThe index can be any `int` expression: a variable, as in `colors.get(i)`, or a calculation, as in `colors.get(i + 1)`.\n\n### Using the values\n\nA value from an `ArrayList<Integer>` can be stored in an `int` variable, or used in a calculation like any `int`:\n\n```java\nArrayList<Integer> points = new ArrayList<>();\npoints.add(8);\npoints.add(5);\npoints.add(10);\nint first = points.get(0);\nint total = points.get(0) + points.get(1) + points.get(2);\nSystem.out.println(\"First round: \" + first);\nSystem.out.println(\"Total: \" + total);\nSystem.out.println(\"Last two: \" + (points.get(1) + points.get(2)));\n```\n\n```output\nFirst round: 8\nTotal: 23\nLast two: 15\n```\n\nMind the parentheses in the last line. Without them, Java joins `\"Last two: \"` and 5 into text first, and then adds 10 to that text: `Last two: 510`.\n\nOne place where a list value is not like an `int`: `==` between two values that both come straight from the list, as in `points.get(0) == points.get(1)`. That compares the two `Integer` objects, not the numbers, and it can be false even when both hold 1000. To compare two values from the list with each other, store them in `int` variables first and compare those, or use `points.get(0).equals(points.get(1))`. Comparing a list value with an `int` variable or a plain number, as in `points.get(0) == 8`, compares the numbers and works as expected.\n","task":"The list holds the runners of a race, in the order they crossed the finish line. Fill in the blanks so the program prints the top three:\n\n```\nGold: Kofi\nSilver: Mei\nBronze: Lars\n```\n","kind":"fill","seed":"import java.util.ArrayList;\n\npublic class Main {\n    public static void main(String[] args) {\n        ArrayList<String> finishers = new ArrayList<>();\n        finishers.add(\"Kofi\");\n        finishers.add(\"Mei\");\n        finishers.add(\"Lars\");\n        finishers.add(\"Ines\");\n        System.out.println(\"Gold: \" + finishers.[[get(0)]]);\n        System.out.println(\"Silver: \" + finishers.get(1));\n        System.out.println(\"Bronze: \" + finishers.get([[2]]));\n    }\n}\n","solution":"import java.util.ArrayList;\n\npublic class Main {\n    public static void main(String[] args) {\n        ArrayList<String> finishers = new ArrayList<>();\n        finishers.add(\"Kofi\");\n        finishers.add(\"Mei\");\n        finishers.add(\"Lars\");\n        finishers.add(\"Ines\");\n        System.out.println(\"Gold: \" + finishers.get(0));\n        System.out.println(\"Silver: \" + finishers.get(1));\n        System.out.println(\"Bronze: \" + finishers.get(2));\n    }\n}\n","hints":["`get` takes the index in parentheses, and the first runner is at index 0.","The third runner is at index 2."],"tests":[{"name":"Output","stdin":"","expect":"Gold: Kofi\nSilver: Mei\nBronze: Lars","hidden":false}],"require":[],"forbid":[],"more":[{"task":"What does this program print? Watch the index calculations, and the `+` signs in the last line.\n","kind":"predict","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(9);\n        numbers.add(2);\n        int i = 1;\n        System.out.println(numbers.get(i));\n        System.out.println(numbers.get(i + 1));\n        System.out.println(numbers.get(0) + numbers.get(2));\n        System.out.println(\"Sum: \" + numbers.get(0) + numbers.get(2));\n    }\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(9);\n        numbers.add(2);\n        int i = 1;\n        System.out.println(numbers.get(i));\n        System.out.println(numbers.get(i + 1));\n        System.out.println(numbers.get(0) + numbers.get(2));\n        System.out.println(\"Sum: \" + numbers.get(0) + numbers.get(2));\n    }\n}\n","hints":["Index 1 holds 9 and index 2 holds 2. Two numbers added together give a number: 4 + 2 is 6.","In the last line the text comes first, so each `+` joins text: `Sum: `, then `4`, then `2`."],"tests":[{"name":"Output","stdin":"","expect":"9\n2\n6\nSum: 42","hidden":false}],"require":[],"forbid":[],"lines":["9","2","6","Sum: 42"]},{"task":"Four people stand in line. Read their names, one per line, into a list (the starter code does this). Then read a place in line, counted the way people count it (1 is the front), and print who stands there. For the input `Ada`, `Ben`, `Cai`, `Dev` and `2` the program prints:\n\n```\nPlace 2: Ben\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> queue = new ArrayList<>();\n        for (int i = 0; i < 4; i++) {\n            queue.add(scanner.nextLine());\n        }\n        int place = 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        ArrayList<String> queue = new ArrayList<>();\n        for (int i = 0; i < 4; i++) {\n            queue.add(scanner.nextLine());\n        }\n        int place = Integer.valueOf(scanner.nextLine());\n        System.out.println(\"Place \" + place + \": \" + queue.get(place - 1));\n    }\n}\n","hints":["Place 1 is index 0, and place 2 is index 1: the index is one less than the place.","`queue.get(place - 1)` is the person at that place."],"tests":[{"name":"Test 1","stdin":"Ada\nBen\nCai\nDev\n2\n","expect":"Place 2: Ben","hidden":false},{"name":"Test 2","stdin":"Ada\nBen\nCai\nDev\n1\n","expect":"Place 1: Ada","hidden":true},{"name":"Test 3","stdin":"Wen\nXavi\nYara\nZoe\n4\n","expect":"Place 4: Zoe","hidden":true},{"name":"Test 4","stdin":"Wen\nXavi\nYara\nZoe\n3\n","expect":"Place 3: Yara","hidden":true}],"require":[{"pattern":"\\bqueue\\.get\\s*\\(","message":"Get the name from the list with `queue.get(...)`."}],"forbid":[]}]}