Mega Code Archive

 
Categories / Java Tutorial / Collections
 

Copying and Cloning Vectors

public Object clone() Cloning a vector with clone() is like making a shallow copy of the vector. A new vector is created with each object reference copied from the original vector. Similar to calling the Vector constructor that accepts a Collection. All elements of the two vectors will effectively point to the same set of objects. import java.util.Collections; import java.util.Vector; public class MainClass {   public static void main(String args[]) {     Vector v1 = new Vector();     v1.add("A");     v1.add("C");     v1.add("B");     Vector v2 = (Vector) v1.clone();     Collections.sort(v2);     System.out.println(v1);     System.out.println(v2);   } } [A, C, B] [A, B, C]