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));
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)));
First round: 8
Total: 23
Last two: 15Mind 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.