Java Utility Library

Java Vector - clone() Method



The java.util.Vector.clone() method returns a clone of the vector.

Syntax

public Object clone()

Parameters

No parameter is required.

Return Value

Returns a clone of the given vector.

Exception

NA

Example:

In the example below, the java.util.Vector.clone() method is used to create a clone of the vector called MyVector.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a vector
    Vector<Integer> MyVector = new Vector<Integer>();

    //populating vector
    MyVector.add(10);
    MyVector.add(20);
    MyVector.add(30);
    
    Vector NewVec = new Vector();
    NewVec = (Vector) MyVector.clone();

    //printing vector
    System.out.println("NewVec contains: " + NewVec);
  }
}

The output of the above code will be:

NewVec contains: [10, 20, 30]

❮ Java.util - Vector