當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java BitSet clone()用法及代碼示例

clone()方法Java.util.BitSet類用於創建現有BitSet的副本。新的BitSet完全等於現有的BitSet,並且僅是先前BitSet的副本。

用法:

Bit_Set.clone()

參數:該方法不帶任何參數。


返回值:該方法僅返回現有BitSet的另一個副本。

下麵的程序演示了Java中BitSet clone()方法的用法。
示例1:

// 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}

示例2:

// 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}


相關用法


注:本文由純淨天空篩選整理自Chinmoy Lenka大神的英文原創作品 BitSet clone() Method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。