Reputation: 47
When I use these lines:
vector.set(i, copyedVector.clone());
vector holds Vector<String>
copyVector holds strings
It gives me an error when I use clone. But when I remove clone, it works fine. How do I make a copy of a vector into the other vector?
Upvotes: 1
Views: 10840
Reputation: 23
Try this. Add data to vectors by yourself.
Vector<T> vector1 = new Vector<T>();
Vector<T> vector2 = new Vector<T>();
vector1.addAll(vector2);
Upvotes: 0
Reputation: 1604
Not sure if this is exactly what you are asking but if you want to copy all the element you can use the addAll method and pass the vector to copy elements from into it:
http://docs.oracle.com/javase/6/docs/api/java/util/Vector.html#addAll(java.util.Collection)
Upvotes: 0
Reputation: 1889
As others have pointed out, it is not clear if you "vector" variable is a Vector of Vectors (Vector<Vector<String>>
) or simply a Vector of Strings (Vector<String>
). Please see the following code snippet.
Vector<String> destVector = new Vector<String>();
Vector<String> sourceVector = new Vector<String>();
sourceVector.add("A");
sourceVector.add("B");
sourceVector.add("C");
destVector.addAll(0,sourceVector);
// If your target vector is a vector of vectors (of strings)
Vector<Vector<String>> destVector2 = new Vector<Vector<String>>();
destVector2.set(0,(Vector<String>)sourceVector.clone());
Also, please note that the clone
method returns an Object
. So you will have to explicitly cast to your desired data type.
Upvotes: 2