java.util.Vector.addAll() Method



Description

This addAll(int index,Collection<? extends E> c) method is another variant of the previous addAll() method. It inserts all of the elements in the specified collection into this list. The insertion starts at the specified position/index and shifts the element currently at that position (if any).The corresponding indices are also increased to shift subsequent elements to the right.The new elements will appear in the list in the same order as they are returned by the specified collection's iterator.

Declaration

Following is the declaration for java.util.Vector.addAll() method

public boolean addAll(int index,Collection<? extends E> c)

Parameters

  • index − This is the index/position where the first element from the specified collection will be inserted.

  • c − This is the collection containing elements to be added to this list .

Return Value

The return type is true if this list is changed as a result of the call.

Exception

  • NullPointerException − The method call will throw this exception if the specified collection is null.

  • IndexOutOfBoundsException − This exception will be thrown if the accessed index/position is out of range (index < 0 || index > size()).

Example

The following example shows the usage of java.util.Vector.addAll(int index,Collection<? extends E> c) method.

package com.tutorialspoint;

import java.util.Vector;

public class VectorDemo {
   public static void main(String[] args) {

      // create two empty Vectors firstvec and secondvec      
      Vector<Integer> firstvec = new Vector<Integer>(4);      
      Vector<Integer> secondvec = new Vector<Integer>(4);
         
      // use add() method to add elements in the secondvec vector
      secondvec.add(5);
      secondvec.add(6);
      secondvec.add(7);
      secondvec.add(8);

      // use add() method to add elements in the firstvec vector
      firstvec.add(1);
      firstvec.add(2);
      firstvec.add(3);
      firstvec.add(4);

      /** use addAll() method to add elements of the 2nd vector at 
      1st element position of the first vector */

      firstvec.addAll(1,secondvec);

      // let us print all the elements available in vector
      System.out.println("Added numbers are :- "); 
      
      for (Integer number : firstvec) {         
         System.out.println("Number = " + number);
      }           
   } 
}

Let us compile and run the above program, this will produce the following result.Check the second list contents are inserted at the 1st element position of the first list content.

Added numbers are :- 
Number = 1
Number = 5
Number = 6
Number = 7
Number = 8
Number = 2
Number = 3
Number = 4
java_util_vector.htm
Advertisements