Copy Elements of ArrayList to Vector - Java Collection Framework

Java examples for Collection Framework:Vector

Description

Copy Elements of ArrayList to Vector

Demo Code


import java.util.ArrayList;
import java.util.Collections;
import java.util.Vector;
 
public class Main {
 
  public static void main(String[] args) {
    ArrayList arrayList = new ArrayList();
   /* ww w  .j ava  2  s.  c o m*/
    arrayList.add("1");
    arrayList.add("4");
    arrayList.add("2");
    arrayList.add("5");
    arrayList.add("3");
   
    Vector v = new Vector();
   
    v.add("A");
    v.add("B");
    v.add("D");
    v.add("E");
    v.add("F");
    v.add("G");
    v.add("H");
   
    System.out.println("Before copy, Vector Contains : " + v);
   
    //copy all elements of ArrayList to Vector using copy method of Collections class
    Collections.copy(v,arrayList);
   
    System.out.println("After Copy, Vector Contains : " + v);  
  }
}

Result


Related Tutorials