Java Vector clone() Method
clone() The method is in Java's Vector An important method provided by the class, used to create and return a Vector shallow copy of the current. This method is inherited from Object class and is implemented in Vector and overridden in the class to implement specific clone behavior.
Syntax format
public Object clone()
Return Value
This method returns a clone (copy) of the current Vector. The return type is Object, which usually needs to be cast to the Vector type.
Method Characteristics
Shallow Copy Characteristics
The clone() method performs a shallow copy, which means:
- A new
Vectorobject is created - Original
Vectorall elements in the are copied into the newVectorMedium - but for the element objects themselves, only references are copied, not new object instances
2.2 Relationship with the Original Vector
The cloned Vector and the originalVector have:
- The same element order
- The same capacity
- The same elements (pointing to the same objects)
- But they are different object instances
Usage examples
Basic Clone Example
Example
import
java.util.Vector
;
public
class
VectorCloneExample
{
public
static
void
main
(
String
[
]
args
)
{
// Create the original Vector
Vector
<
String
original
=
new
Vector
<>
(
)
;
original.
add
(
"Java"
)
;
original.
add
(
"Python"
)
;
original.
add
(
"C++"
)
;
// Clone the Vector
Vector
<
String
cloned
=
(
Vector
<
String
)
original.
clone
(
)
;
// Output result
System
.
out
.
println
(
"Original Vector: "
original
)
;
System
.
out
.
println
(
"Cloned Vector: "
cloned
)
;
// Modify the original Vector
original.
add
(
"JavaScript"
)
;
// Output again
System
.
out
.
println
(
"\nAfter modifying original:"
)
;
System
.
out
.
println
(
"Original Vector: "
original
)
;
System
.
out
.
println
(
"Cloned Vector: "
cloned
)
;
}
}
Output result:
Original Vector: [Java, Python, C++]
Cloned Vector: [Java, Python, C++]
After modifying original:
Original Vector: [Java, Python, C++, JavaScript]
Cloned Vector: [Java, Python, C++]
Shallow Copy Verification Example
Example
import
java.util.Vector
;
class
Person
{
String
name
;
Person
(
String
name
)
{
this
.
name
=
name
;
}
@Override
public
String
toString
(
)
{
return
name
;
}
}
public
class
ShallowCopyDemo
{
public
static
void
main
(
String
[
]
args
)
{
Vector
<
Person
people
=
new
Vector
<>
(
)
;
people.
add
(
new
Person
(
"Alice"
)
)
;
people.
add
(
new
Person
(
"Bob"
)
)
;
Vector
<
Person
clonedPeople
=
(
Vector
<
Person
)
people.
clone
(
)
;
// Modify the properties of the Person object in the original Vector
people.
get
(
0
)
.
name
=
"Charlie"
;
System
.
out
.
println
(
"Original: "
people
)
;
System
.
out
.
println
(
"Cloned: "
clonedPeople
)
;
}
}
Output result:
Original: [Charlie, Bob]
Cloned: [Charlie, Bob]
Notes
Type Conversion
Because the clone() method returns the Object type, a cast is needed when using it:
Vector cloned = (Vector) original.clone();
Concurrency issues
Although Vector is thread-safe, if another thread modifies the original Vector during cloning, an inconsistent state may result. The clone operation should be synchronized when necessary.
Performance considerations
For large Vectors, the clone operation may consume a lot of memory and CPU resources and should be used with caution.
Alternatives
Using a Constructor
A copy can be created through the Vector constructor:
Vector copy = new Vector<>(original);
Use the addAll method
First create a new Vector, then add all elements:
Example
Vector
<
String
copy
=
new
Vector
<>
(
)
;
copy.
addAll
(
original
)
;
Deep Copy Implementation
If a deep copy is needed, you can implement it manually:
Example
Vector
<
Person
deepCopy
=
new
Vector
<>
(
)
;
for
(
Person p
:
original
)
{
deepCopy.
add
(
new
Person
(
p.
name
)
)
;
}
Summary
Vector 's clone() The method provides a quick way to create Vector copies, but you need to note its shallow-copy characteristic. In actual development, choose an appropriate copy method based on specific needs, and consider factors such as thread safety and performance.
Java Vector