![]() |
VOOZH | about |
In Java, the clone() method of the Vector class is used to return a shallow copy of the vector. 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.
Example 1: In this example, we use the clone() method to create the shallow copy of a vector of integer type.
The Original Vector is: [100, 200, 300] The Cloned Vector is: [100, 200, 300]
() Methodpublic Object clone()
Return Type: It returns an object which is just the copy of the vector.
Example 2: In this example, we create a shallow copy of a vector of string type.
The Original Vector is: [Geeks, For, Geeks] The Cloned Vector is: [Geeks, For, Geeks]
Example 3: To better understand shallow copying, here's an example using a vector containing mutable objects.
The Original Vector is: [Hello, Geeks] The Cloned Vector is: [Hello, Geeks] After modification: The Original Vector is: [Hello Java, Geeks] The Cloned Vector is: [Hello Java, Geeks]