The .set() method replaces the element at a given index with a new value:
ArrayList<String> colors = new ArrayList<>();
colors.add("red");
colors.add("green");
colors.add("blue");
colors.set(1, "yellow");
After this call, the list is ["red", "yellow", "blue"]. The old value "green" is gone.
.set() also returns the old value it replaced. You can capture it if you need it:
String old = colors.set(1, "yellow");
System.out.println(old); // "green"
If the index is out of bounds, Java throws an IndexOutOfBoundsException. Unlike .add(), .set() does not change the list's size. It only swaps one element for another.