Java Vector - addAll() Method
The java.util.Vector.addAll() method is used to insert all elements of the specified collection at the specified position in the vector. The method shifts the current element at the specified position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in the vector in the order that they are returned by the specified collection's iterator.
Syntax
public boolean addAll(int index, Collection<? extends E> c)
Here, E is the type of element maintained by the container.
Parameters
index |
Specify index number at which the first element of collection need to be inserted in the vector. |
c |
Specify the collection containing all elements which need to be added in the vector. |
Return Value
Returns true if the vector changed as a result of the call, else returns false.
Exception
Throws IndexOutOfBoundsException, if the index is out of range i.e., (index < 0 || index > size()).
Throws NullPointerException, if the specified collection is null.
Example:
In the example below, the java.util.Vector.addAll() method is used to insert all elements of the vector vec at the specified position in the vector MyVector.
import java.util.*; public class MyClass { public static void main(String[] args) { //creating a vector Vector<Integer> MyVector = new Vector<Integer>(); Vector<Integer> vec = new Vector<Integer>(); //populating MyVector MyVector.add(10); MyVector.add(20); MyVector.add(30); //populating vec vec.add(100); vec.add(200); //printing vector System.out.println("Before method call, MyVector contains: " + MyVector); MyVector.addAll(1, vec); //printing vector System.out.println("After method call, MyVector contains: " + MyVector); } }
The output of the above code will be:
Before method call, MyVector contains: [10, 20, 30] After method call, MyVector contains: [10, 100, 200, 20, 30]
❮ Java.util - Vector