The size of a list
size() tells how many values a list holds right now. A new list has size 0, and every add makes it one bigger:
ArrayList<String> guests = new ArrayList<>();
System.out.println(guests.size());
guests.add("Rosa");
guests.add("Tariq");
guests.add("Yuki");
System.out.println(guests.size());
System.out.println("Last to arrive: " + guests.get(guests.size() - 1));
0
3
Last to arrive: YukiSince 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.)
Reading an unknown number of values
size() is what makes lists so handy for input: the program reads as many values as it's given, and counts them afterwards.
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<String> trees = new ArrayList<>();
while (true) {
String tree = scanner.nextLine();
if (tree.equals("end")) {
break;
}
trees.add(tree);
}
System.out.println("Trees seen: " + trees.size());
if (trees.size() > 0) {
System.out.println("First: " + trees.get(0));
System.out.println("Last: " + trees.get(trees.size() - 1));
}
}
}
maple
oak
birch
endTrees seen: 3
First: maple
Last: birchChecking an index
With 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:
if (index >= 0 && index < trees.size()) {
System.out.println(trees.get(index));
} else {
System.out.println("No tree at index " + index);
}