Monday 25 May 2015

How to add all elements of a list to Vector

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

public class AddAllElement {
   
    public static void main(String[] args) {
      
        Vector<String> vector = new Vector<String>();
        //add element in vector
        vector.add("cricket");
        vector.add("hockey");
        vector.add("football");
        vector.add("tennish");
      
        // print vector
        System.out.println("Vector = "+vector);
      
        List<String> list =  new ArrayList<>();
        list.add("java");
        list.add("c++");
        list.add("php");
      
        System.out.println("list = "+list);
      
        /*
         * Appends all of the elements in the specified
         * Collection to the end of this Vector, in the
         * order that they are returned by the specified
         *  Collection's Iterator.
         */

        vector.addAll(list);
      
       System.out.println("vector after add list ="+vector);
      
    }

}

Output:

Vector = [cricket, hockey, football, tennish]

list = [java, c++, php]

vector after add list =[cricket, hockey, football, tennish, java, c++, php]



No comments:

Post a Comment