| Learn | Java - Lists - Positional access operations |
|---|
| Positional Access Operations The List API has eight methods for positional access operations:
So, if we have a List: And we wanted to add the strings "Hello world!" and "Goodbye world!" to it, we would do it as such: 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: NOTE: The first element is 0. Now, if we wanted to remove the "Goodbye world!" line, we could do it like this: And if we wanted to remove the first line (which in this case would be "Program starting!", we could do it like this: Note: In order to retrieve an element of the list at a specified position you can use the will return the first element of the list. You can replace any element at a specified position by using the This will set the String "This is a replacement" as the first element of the list. Note: The set method will overwrite the element at the position 0. It will not add the new String at the position 0 and push the old one to the position 1. The the 0 is expected to be returned as we set the String "This is a replacement" in the position 0 of our list.
In case where there are more than one occurrence in the list when This time the 1 will be returned and not the 0; |
Read Article
https://develop.horje.com/learn/1434/reference