import java.util.Collections;Output:
import java.util.Vector;
public class Copy {
public static void main(String[] args) {
Vectorv1 = new Vector ();
v1.add("a");
v1.add("b");
v1.add("c");
v1.add("d");
v1.add("e");
Vectorv2 = new Vector (5);
v2.add("a2");
v2.add("b2");
v2.add("c2");
v2.add("d2");
v2.add("e2");
System.out.println("v2(before): "+v2);
Collections.copy(v2, v1);
System.out.println("v2(after): "+v2);
}
}
v2(before): [a2, b2, c2, d2, e2]
v2(after): [a, b, c, d, e]
Note: The Collections.copy() throws an exception if the second vector v2 is empty, or its size doesn't match the size of v1, because this method copies subscript by subscript without allocating memory and incrementing the capacity of v1. |
Please disable your ad blocker and refresh the window to use this website.