You can loop through an ArrayList with a standard for loop, just like an array:
ArrayList<String> names = new ArrayList<>();
names.add("Alice");
names.add("Bob");
names.add("Carol");
for (int i = 0; i < names.size(); i++) {
System.out.println(names.get(i));
}
This prints each name on its own line. The loop runs from index to names.size() - 1.
Use this style when you need the index during iteration. For example, if you want to print both the position and the value, the index variable i is already available. If you do not need the index, the for-each loop in the next unit is shorter and less error-prone.