📜  Java中的向量 clone() 方法及示例

📅  最后修改于: 2022-05-13 01:54:44.443000             🧑  作者: Mango

Java中的向量 clone() 方法及示例

Vector 类的clone()方法用于返回 Object 类提供的该向量浅表副本。它只是创建向量的副本。副本将引用内部数据数组的克隆,但不会引用原始内部数据数组。

句法:

Vector.clone()

参数:该方法不带任何参数。

返回值:该方法返回一个对象,它只是向量的副本。

抛出异常: CloneNotSupportedException: 如果对象的类不支持 Cloneable 接口,则会引发此异常。

示例 1:

Java
// Java Program to Illustrate clone() method of Vector class
 
// Importing required classes
import java.util.*;
 
// Main class
// VectorDemo
public class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
 
        // Creating an empty Vector by creating object
        // of vector class of string type
        Vector vec_tor = new Vector();
 
        // Adding custom input elements into the vector
        // using add() method
        vec_tor.add("Welcome");
        vec_tor.add("To");
        vec_tor.add("Geeks");
        vec_tor.add("For");
        vec_tor.add("Geeks");
 
        // Print and display all vector elements
        System.out.println("Vector: " + vec_tor);
 
        // Creating another vector to copy by
        // creating an object of Object class
        Object copy_vector = vec_tor.clone();
 
        // Print and display the cloned vector elements
        System.out.println("The cloned vector is: "
                           + copy_vector);
    }
}


Java
// Java Program to Illustrate clone() method of Vector class
 
// Importing required classes
import java.util.*;
 
// Main class
// VectorDemo
public class VectorDemo {
   
    // Main driver method
    public static void main(String args[])
    {
        // Creating an empty Vector by creating
       // object of Vector class of Integer type
        Vector vec_tor = new Vector();
 
        // Add custom input elements for 
        // using add() method
        vec_tor.add(10);
        vec_tor.add(15);
        vec_tor.add(30);
        vec_tor.add(20);
        vec_tor.add(5);
 
        // Print and display the vector elements
        System.out.println("Vector: " + vec_tor);
 
        // Creating another vector to copy by
        // creating object of Object class
        Object copy_vector = (Vector)vec_tor.clone();
 
        // Print and display elements of cloned vector
        System.out.println("The cloned vector is: " + copy_vector);
    }
}


输出
Vector: [Welcome, To, Geeks, For, Geeks]
The cloned vector is: [Welcome, To, Geeks, For, Geeks]

示例 2:

Java

// Java Program to Illustrate clone() method of Vector class
 
// Importing required classes
import java.util.*;
 
// Main class
// VectorDemo
public class VectorDemo {
   
    // Main driver method
    public static void main(String args[])
    {
        // Creating an empty Vector by creating
       // object of Vector class of Integer type
        Vector vec_tor = new Vector();
 
        // Add custom input elements for 
        // using add() method
        vec_tor.add(10);
        vec_tor.add(15);
        vec_tor.add(30);
        vec_tor.add(20);
        vec_tor.add(5);
 
        // Print and display the vector elements
        System.out.println("Vector: " + vec_tor);
 
        // Creating another vector to copy by
        // creating object of Object class
        Object copy_vector = (Vector)vec_tor.clone();
 
        // Print and display elements of cloned vector
        System.out.println("The cloned vector is: " + copy_vector);
    }
}
输出
Vector: [10, 15, 30, 20, 5]
The cloned vector is: [10, 15, 30, 20, 5]