share

Thursday, 21 May 2015

How To Shallow Copy Or Clone a Vector

import java.util.Vector;

public class CloneOfVector {
   
    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);
   
        /*
         * Returns a clone of this vector. The copy will
         * contain a reference to a clone of the internal
         *  data array, not a reference to the original internal
         *  data array of this Vector object.
         */
      
        Vector<String> myvector = (Vector<String>) vector.clone();
      
        for (String str : myvector) {

        System.out.println("element is = "+str);
   
         }
      
    }

}

No comments:

Post a Comment