java.util.Vector.addAll() Method


Description

The addAll(Collection<? extends E> c) method is used to append all of the elements in the specified collection to the end of the list.The order will be same as they are returned by the specified collection's Iterator. This method should not be called at the same time while modifying the collection.This will cause undefined behavior and incorrect result.

Declaration

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

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

Parameters

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

Example

The following example shows the usage of java.util.Vector.addAll(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 secondvec with firstvec vector
      firstvec.addAll(secondvec);

      // let us print all the elements available in vector firstvec 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 appended with first list content.

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

Get certified by completing the course

Get Started
Advertisements