Java Arena
Step 3 of 8

Getting values by index

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:

ArrayList<String> colors = new ArrayList<>();
colors.add("red");
colors.add("green");
colors.add("blue");
System.out.println(colors.get(0));
System.out.println(colors.get(2));
System.out.println(colors.get(1));
Output
red
blue
green
index 0 1 2
value red green blue

So 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.

The index can be any int expression: a variable, as in colors.get(i), or a calculation, as in colors.get(i + 1).

Using the values

A value from an ArrayList<Integer> can be stored in an int variable, or used in a calculation like any int:

ArrayList<Integer> points = new ArrayList<>();
points.add(8);
points.add(5);
points.add(10);
int first = points.get(0);
int total = points.get(0) + points.get(1) + points.get(2);
System.out.println("First round: " + first);
System.out.println("Total: " + total);
System.out.println("Last two: " + (points.get(1) + points.get(2)));
Output
First round: 8
Total: 23
Last two: 15

Mind 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.

One 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.

Your task

Challenge 1 of 3

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:

Gold: Kofi
Silver: Mei
Bronze: Lars

Challenge 2

What does this program print? Watch the index calculations, and the + signs in the last line.

Challenge 3

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:

Place 2: Ben