How to copy all elements of Java Vector object to an array using copyInTo method. - Java Collection Framework

Java examples for Collection Framework:Vector

Description

How to copy all elements of Java Vector object to an array using copyInTo method.

Demo Code


import java.util.Vector;
 
public class Main {
 
  public static void main(String[] args) {
    Vector v = new Vector();
   /* w w w.ja v a2 s.com*/
    v.add("1");
    v.add("2");
    v.add("3");
    v.add("4");
    v.add("5");
   
    //declare an array to hold elements of Vector
    Object[] objArray = new Object[5];
   
    v.copyInto(objArray);
   
    System.out.println("Vector elements are copied into an Array. Now Array Contains..");
    for(int index=0; index < objArray.length ; index++)
      System.out.println(objArray[index]);
  }
}

Result


Related Tutorials