1. Java List add[]
This method is used to add elements to the list. There are two methods to add elements to the list.
- add[E e]: appends the element at the end of the list. Since List supports Generics, the type of elements that can be added is determined when the list is created.
- add[int index, E element]: inserts the element at the given index. The elements from the given index is shifted towards right of the list. The method throws IndexOutOfBoundsException if the given index is out of range.
Lets look at some examples of List add[] methods.
Recommended Readings:
- Java List
- Java ArrayList
2. Java List addAll[]
This method is used to add the elements from a collection to the list. There are two overloaded addAll[] methods.
- addAll[Collection