The List API has eight methods for positional access operations:
add(T type)add(int index, T type)remove(Object o)remove(int index)get(int index)set(int index, E element)int indexOf(Object o)int lastIndexOf(Object o)So, if we have a List:
List<String> strings = new ArrayList<String>();
And we wanted to add the strings “Hello world!” and “Goodbye world!” to it, we would do it as such:
strings.add("Hello world!");
strings.add("Goodbye world!");
And our list would contain the two elements. Now lets say we wanted to add “Program starting!” at the front of the list. We would do this like this:
strings.add(0, "Program starting!");
NOTE: The first element is 0.
Now, if we wanted to remove the “Goodbye world!” line, we could do it like this:
strings.remove("Goodbye world!");
And if we wanted to remove the first line (which in this case would be “Program starting!”, we could do it like this:
strings.remove(0);