Home » Java programming language

Java Vector clone() Method with Example

Vector Class clone() method: Here, we are going to learn about the clone() method of Vector Class with its syntax and example.
Submitted by Preeti Jain, on March 15, 2020

Vector Class clone() method

  • clone() method is available in java.util package.
  • clone() method is used to copy or clone or return a shallow copy of this Vector.
  • clone() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • clone() method may throw an exception at the time of cloning an object.
    CloneNotSupportedException: This exception may throw when this Vector class unsupported a Cloneable interface.

Syntax:

    public Object clone();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of the method is Object, it returns cloned copy of this Vector.

Example:

// Java program to demonstrate the example 
// of Object clone() method of Vector 

import java.util.Vector;

public class CloneOfVector {
    public static void main(String[] args) {
        // Instantiates a Vector object  with
        // initial capacity of "10"
        Vector < String > v = new Vector < String > (10);
        Vector < String > clone_v = new Vector < String > (10);

        // By using add() method is to add the
        // elements in this v
        v.add("C");
        v.add("C++");
        v.add("JAVA");

        // Display Vector 
        System.out.println("v: " + v);

        // By using clone() method is to
        // clone this vector v
        clone_v = (Vector) v.clone();

        // Display Cloned Vector
        System.out.println("v.clone(): " + clone_v);
    }
}

Output

v: [C, C++, JAVA]
v.clone(): [C, C++, JAVA]



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.