This example shows how to clone, copy, or append Vector in Java. This example also shows how to clone, append one vector to another, and copy vector using constructor, addAll method, and clone method.
How to clone, copy, or append one Vector to another in Java?
There are several ways using which we can copy one vector to another in Java.
1. Using the constructor
The Vector class provides a constructor that accepts a collection object as a parameter.
1 |
public Vector(Collection<? extends E> collection) |
It creates a new vector object containing all the elements of the specified collection object. We can use this constructor to copy one vector object to another as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.Vector; public class CopyCloneAppendVector { public static void main(String[] args) { Vector<String> vColors = new Vector<String>(); vColors.add("Black"); vColors.add("Orange"); vColors.add("Yellow"); //use the constructor Vector<String> vColorsCopied = new Vector<String>( vColors ); System.out.println("Copied vector contains: " + vColorsCopied); } } |
Output
1 |
Copied vector contains: [Black, Orange, Yellow] |
2. Copy or append one vector to another using the addAll method
The Vector addAll
method adds all elements of the specified collection object to this vector object.
1 |
public boolean addAll(Collection<? extends E> collection) |
It returns true if this vector object is changed as a result of this method call.
We can use this method to append elements of one vector to another or copy a vector.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
Vector<Integer> vNumbers1 = new Vector<Integer>(); vNumbers1.add(1); vNumbers1.add(2); Vector<Integer> vNumbers2 = new Vector<Integer>(); vNumbers2.add(3); vNumbers2.add(4); /* * Use the addAll method to append elements * of one vector to another */ //this will append elements of vNumbers2 to vNumbers1 vNumbers1.addAll(vNumbers2); System.out.println("Elements appended: " + vNumbers1); |
Output
1 |
Elements appended: [1, 2, 3, 4] |
3. Using the clone method
The Vector clone
method returns a clone of this vector object.
1 |
public Object clone() |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Vector<Integer> vNumbers = new Vector<Integer>(); vNumbers.add(1); vNumbers.add(2); vNumbers.add(3); /* * To clone the vector object, use * the clone method */ Vector<Integer> vNumbersCloned = (Vector<Integer>) vNumbers.clone(); System.out.println("Cloned vector contains: " + vNumbersCloned); |
Output
1 |
Cloned vector contains: [1, 2, 3] |
Important Note:
All of the above given approaches create a shallow copy of the vector object. That means only the element object references are copied, not the actual objects.
If you change the element object, the change will be reflected in the original as well as cloned or copied vector object as shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
import java.util.Vector; class Employee{ private int id; private String name; public Employee(int id, String name){ this.id = id; this.name = name; } public void setName(String name){ this.name = name; } public String toString(){ return "{" + this.name + " -> " + this.id + "}"; } } public class CopyCloneAppendVector { public static void main(String[] args) { Employee emp1 = new Employee(1, "John"); Employee emp2 = new Employee(2, "Raj"); Employee emp3 = new Employee(3, "Emily"); Vector<Employee> vEmployees = new Vector<Employee>(); vEmployees.add(emp1); vEmployees.add(emp2); vEmployees.add(emp3); //clone the vector Vector<Employee> vEmployeesCloned = (Vector<Employee>) vEmployees.clone(); System.out.println("Original Vector: " + vEmployees); System.out.println("Cloned Vector: " + vEmployeesCloned); //change an element object emp1.setName("JACK"); //the change will be reflected in both the vector objects System.out.println("Original Vector: " + vEmployees); System.out.println("Cloned Vector: " + vEmployeesCloned); } } |
Output
1 2 3 4 |
Original Vector: [{John -> 1}, {Raj -> 2}, {Emily -> 3}] Cloned Vector: [{John -> 1}, {Raj -> 2}, {Emily -> 3}] Original Vector: [{JACK -> 1}, {Raj -> 2}, {Emily -> 3}] Cloned Vector: [{JACK -> 1}, {Raj -> 2}, {Emily -> 3}] |
This example is a part of the Java Vector Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 Vector Documentation