Java Vector clone() Method



Description

The Java Vector clone() method is used to get another copy of an existing 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.

Declaration

Following is the declaration for java.util.Vector.clone() method

public Object clone()

Parameters

  • This method does not take any parameter as input.

Return Value

Object − The method call returns a clone of this vector as an object.

Exception

CloneNotSupportedException − This exception is thrown if the object's class does not support the Cloneable interface.

Getting Clone of a Vector of Integer Example

The following example shows the usage of Java Vector clone() method. In this example, we're using Integers. At first, we're creating a vector as a new Vector object and then initialize with few items. As next step, we're cloning the vector1 to vector2 using clone() method call on vector1 object. In the end, we're printing vector2 to check if it contains copy of all elements of vector2 object.

package com.tutorialspoint;

import java.util.Vector;

public class VectorDemo {
   public static void main(String[] args) {
      
      // create an empty vector
      Vector<Integer> vector1 = new Vector<>();

      // use add() method to add elements in the vector
      vector1.add(1);
      vector1.add(2);
      vector1.add(3);
      vector1.add(4);
         
      // clone the first vector,
      Vector<Integer> vector2 = (Vector<Integer>)vector1.clone();

      // let us print all the elements available in vector2
      // now vector2 should have similar elements to vector1.
      System.out.println("Vector = " + vector2);
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Vector = [1, 2, 3, 4]

Getting Clone of a Vector of String Example

The following example shows the usage of Java Vector clone() method. In this example, we're using Strings. At first, we're creating a vector as a new Vector object and then initialize with few items. As next step, we're cloning the vector1 to vector2 using clone() method call on vector1 object. In the end, we're printing vector2 to check if it contains copy of all elements of vector2 object.

package com.tutorialspoint;

import java.util.Vector;

public class VectorDemo {
   public static void main(String[] args) {
      
      // create an empty vector
      Vector<String> vector1 = new Vector<>();

      // use add() method to add elements in the vector
      vector1.add("A");
      vector1.add("B");
      vector1.add("C");
      vector1.add("D");
         
      // clone the first vector,
      Vector<String> vector2 = (Vector<String>)vector1.clone();

      // let us print all the elements available in vector2
      // now vector2 should have similar elements to vector1.
      System.out.println("Vector = " + vector2);
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Vector = [A, B, C, D]

Getting Clone of a Vector of Object Example

The following example shows the usage of Java Vector clone() method. In this example, we're using Student objects. At first, we're creating a vector as a new Vector object and then initialize with few items. As next step, we're cloning the vector1 to vector2 using clone() method call on vector1 object. In the end, we're printing vector2 to check if it contains copy of all elements of vector2 object.

package com.tutorialspoint;

import java.util.Vector;

public class VectorDemo {
   public static void main(String[] args) {
      
      // create an empty vector
      Vector<Student> vector1 = new Vector<>();

      // use add() method to add elements in the vector
      vector1.add(new Student(1, "Julie"));
      vector1.add(new Student(2, "Robert"));
      vector1.add(new Student(3, "Adam"));
         
      // clone the first vector,
      Vector<Student> vector2 = (Vector<Student>)vector1.clone();

      // let us print all the elements available in vector2
      // now vector2 should have similar elements to vector1.
      System.out.println("Vector = " + vector2);
   }
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
} 

Output

Let us compile and run the above program, this will produce the following result −

Vector = [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]]
java_util_vector.htm
Advertisements