Java Vector clone() Method Last Updated : 14 Jan, 2025 Comments Improve Suggest changes Like Article Like Report 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. Java // Demonstrating the use of clone() method // with a Vector of Integer type import java.util.Vector; class Geeks { public static void main(String[] args) { // Creating a vector Vector<Integer> v = new Vector<>(); // using add() to insert elements in the vector v.add(100); v.add(200); v.add(300); System.out.println("The Original Vector is: " + v); // Cloning the original vector Vector<Integer> cv = (Vector<Integer>)v.clone(); // Displaying the cloned vector System.out.println("The Cloned Vector is: " + cv); } } OutputThe Original Vector is: [100, 200, 300] The Cloned Vector is: [100, 200, 300] Syntax of Vector clone() 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. Java // Demonstrating the use of clone() method // with a Vector of String type import java.util.Vector; public class Geeks { public static void main(String args[]) { // Creating a vector Vector<String> v = new Vector<String>(); // using add() method to insert // elements in the vector v.add("Geeks"); v.add("For"); v.add("Geeks"); System.out.println("The Original Vector is: " + v); // Cloning the Original vector Vector<String> cv = (Vector<String>)v.clone(); // Displaying the Cloned vector System.out.println("The Cloned Vector is: " + cv); } } OutputThe 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. Java // Demonstrating the use of clone() method with // a Vector of StringBuilder objects import java.util.Vector; public class Geeks { public static void main(String[] args) { // Creating a vector of StringBuilder objects Vector<StringBuilder> vec = new Vector<>(); vec.add(new StringBuilder("Hello")); vec.add(new StringBuilder("Geeks")); System.out.println("The Original Vector is: " + vec); // Cloning the original vector Vector<StringBuilder> cv = (Vector<StringBuilder>) vec.clone(); System.out.println("The Cloned Vector is: " + cv); // Modifying an element in the original vector vec.get(0).append(" Java"); System.out.println("After modification: "); System.out.println("The Original Vector is: " + vec); System.out.println("The Cloned Vector is: " + cv); } } OutputThe 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] Comment More infoAdvertise with us K kundankumarjha Follow Improve Article Tags : Misc Java Java-Collections Java - util package Java-Functions Java-Vector +2 More Practice Tags : JavaJava-CollectionsMisc Similar Reads Vector Class in Java The Vector class in Java implements a growable array of objects. Vectors were legacy classes, but now it is fully compatible with collections. It comes under java.util package and implement the List interface.Key Features of Vector:It expands as elements are added.Vector class is synchronized in nat 12 min read Vector add() Method in Java To add an element to a Vector in Java, you can use the add() method, which appends an element to the end of the vector.Implementation:Java// Java code to illustrate Adding // Element in Vector import java.util.*; public class GFG { public static void main(String args[]) { // Creating an empty Vector 3 min read Vector set() Method in Java The Java.util.Vector.set() method is used to replace any particular element in the vector, created using the Vector class, with another element. Syntax: Vector.set(int index, Object element) Parameters: This function accepts two mandatory parameters as shown in the above syntax and described below. 2 min read Vector remove() Method in Java remove(int index) The java.util.vector.remove(int index) method is used to remove an element from a Vector from a specific position or index. Syntax: Vector.remove(int index) Parameters: This method accepts a mandatory parameter index is of integer data type and specifies the position of the element 2 min read Vector get() Method in Java The java.util.vector.get() method is used to fetch or retrieve an element at a specific index from a Vector. Syntax: Vector.get(int index) Parameters: This method accepts a mandatory parameter index which is of integer data type. It specifies the position or index of the element to be fetched from t 2 min read Vector addAll() Method in Java In Java, the addAll() method is used to add all elements from a specified Collection to the current Vector. The addAll() method allows you to efficiently append multiple elements to a vector in one operation.Implementation:Java// Java Program to addAll elements // From Vector to Vector import java.u 3 min read Vector addElement() Method in Java The Java.util.Vector.addElement() method is used to append a specified element to the end of this vector by increasing the size of the vector by 1. The functionality of this method is similar to that of the add() method of Vector class.Syntax: boolean addElement(Object element)Parameters: This funct 2 min read Vector capacity() Method in Java The Java.util.Vector.capacity() method in Java is used to get the capacity of the Vector or the length of the array present in the Vector.Syntax of Vector capacity() methodVector.capacity()Parameters: The method does not take any parameter. Return Value: The method returns the capacity or the intern 2 min read Vector clear() Method in Java The Java.util.Vector.clear() method is used to remove all the elements from a Vector. Using the clear() method only clears all the element from the vector and does not delete the vector. In other words, we can say that the clear() method is used to only empty an existing vector. Syntax:Vector.clear( 2 min read Java Vector clone() Method 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 cl 3 min read Like