Java Vector clone() Method

Java Vector Java Vector

clone()The method is in JavaVectoran important method provided by the classVectora shallow copy of theObjectclass, and inVectorthe class is overridden to implement specific cloning behavior.

Syntax

public Object clone()

Return Value

This method returns a clone (copy) of the current Vector. The return type is Object, and it usually needs to be cast to Vector type.


Method Features

Shallow Copy Feature

The clone() method performs a shallow copy, which means:

  • A newVectorobject will be created
  • the originalVectorAll elements in the original will be copied to the newVectorMedium
  • However, for the element objects themselves, only references are copied, not new object instances created.

2.2 Relationship with the Original Vector

The cloned Vectorand the originalVectorhas:

  • Same element order
  • Same capacity
  • Same elements (pointing to the same objects)
  • But they are different object instances

Usage Examples

Basic Cloning 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 the 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 attribute 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 Casting

Since the clone() method returns an Object type, a forced type cast is required when using it:

Vector<String> cloned = (Vector<String>) original.clone();

Concurrency Issues

Although Vector is thread-safe, if other threads modify the original Vector during the cloning process, it may lead to an inconsistent state. If necessary, you should synchronize the cloning operation.

Performance Considerations

For largeVector, the cloning operation may consume more memory and CPU resources, so it should be used with caution.


Alternatives

Using a Constructor

You canVectorconstructor to create a copy:

Vector<String> copy = new Vector<>(original);

Using the addAll Method

First create a newVector, 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

Vectorofclone()The method provides a quick way to createVectorIt is a way to create copies, but you need to pay attention to its shallow copy nature. In actual development, you should choose an appropriate copy method based on specific needs, and consider factors such as thread safety and performance.

Java Vector Java Vector

Other Extensions