AbstractList addAll() method in Java with Examples
Last Updated :
30 Sep, 2019
The
addAll() method of
java.util.AbstractList class is used to insert all of the elements in the specified collection into this list at the specified position.
- This shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices).
- The new elements will appear in this list in the order that they are returned by the specified collection's iterator.
- The behavior of this operation is undefined if the specified collection is modified while the operation is in progress.
This implementation gets an iterator over the specified collection and iterates over it, inserting the elements obtained from the iterator into this list at the appropriate position, one at a time, using add(int, E). Many implementations will override this method for efficiency.
Syntax:
public boolean addAll(int index, Collection c)
Parameters: This method takes the following argument as a parameter.
- index- the index at which to insert the first element from the specified collection
- c- the collection containing elements to be added to this list
-
Return Value: This method returns true if this list changed as a result of the call.
-
Exception: This method throws the following Exception.
- NullPointerException - if the specified collection contains one or more null elements and this list does not permit null elements, or if the specified collection is null
- IndexOutOfBoundsException - if the index is out of range (index size()).
Below are the examples to illustrate the
addAll() method.
Example 1:
Java