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

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

Java中的 BitSet clone() 方法及示例

clone() 方法Java.util.BitSet 类用于创建现有 BitSet 的副本。新的 BitSet 与现有的 BitSet 完全相同,并且仅仅是先前 BitSet 的副本。

句法:

Bit_Set.clone()

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

返回值:该方法只返回现有 BitSet 的另一个副本。

下面的程序说明了Java中 BitSet clone() 方法的工作原理。
方案一:

// Java code to illustrate clone()
import java.util.*;
  
public class BitSet_Demo {
    public static void main(String args[])
    {
        // Creating an empty BitSet
        BitSet init_bitset = new BitSet();
  
        // Use set() method to add elements into the Set
        init_bitset.set(10);
        init_bitset.set(20);
        init_bitset.set(30);
        init_bitset.set(40);
        init_bitset.set(50);
  
        // Displaying the BitSet
        System.out.println("Initial BitSet: " + init_bitset);
  
        // Creating a new cloned set
        BitSet cloned_set = new BitSet();
  
        // Cloning the set using clone() method
        cloned_set = (BitSet)init_bitset.clone();
  
        // Displaying the new Set after Cloning
        System.out.println("The new BitSet: " + cloned_set);
    }
}
输出:
Initial BitSet: {10, 20, 30, 40, 50}
The new BitSet: {10, 20, 30, 40, 50}

方案二:

// Java code to illustrate clone()
import java.util.*;
  
public class BitSet_Demo {
    public static void main(String args[])
    {
        // Creating an empty BitSet
        BitSet init_bitset = new BitSet();
  
        // Use set() method to add elements into the Set
        init_bitset.set(40);
        init_bitset.set(25);
        init_bitset.set(80);
        init_bitset.set(95);
        init_bitset.set(5);
  
        // Displaying the BitSet
        System.out.println("Initial BitSet: " + init_bitset);
  
        // Creating a new cloned set
        BitSet cloned_set = new BitSet();
  
        // Cloning the set using clone() method
        cloned_set = (BitSet)init_bitset.clone();
  
        // Displaying the new Set after Cloning
        System.out.println("The new BitSet: " + cloned_set);
    }
}
输出:
Initial BitSet: {5, 25, 40, 80, 95}
The new BitSet: {5, 25, 40, 80, 95}