Vector clone() Method in Java with Examples
The clone() method of the Vector class is used to return a shallow copy of this vector provided by the Object class. It just creates a copy of the vector. The copy will have a reference to a clone of the internal data array but not a reference to the original internal data array.
Parameters: The method does not take any parameters.
Return Value: The method returns an Object which is just the copy of the vector.
Exception Thrown: CloneNotSupportedException: This exception is thrown if the object’s class does not support the Cloneable interface.
Vector: [Welcome, To, Geeks, For, Geeks] The cloned vector is: [Welcome, To, Geeks, For, Geeks]
Vector: [10, 15, 30, 20, 5] The cloned vector is: [10, 15, 30, 20, 5]
Note: In the above examples we have cloned vectors by creating objects of Object class itself as it does provide clone() method. So if object classes are not supporting any cloneable interface then it will not allow to clone and copy vector elements so does it will throw CloneNotSupportedException.