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


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


java.util.BitSet.toLongArray()是BitSet類的內置方法,用於生成一個新的長數組,其中包含現有BitSet的所有位。根據官方文檔,此過程以以下方式工作:

if, long[] longs = bit_set.toLongArray();
then, longs.length == (bit_set.length()+63)/64 and,
bit_set.get(n) == ((longs[n/64] & (1L

用法:


Bit_Set.toLongArray()

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

返回值:該方法返回由給定BitSet的元素的LongArray表示形式組成的集合。

以下程序說明了java.util.BitSet.toLongArray()方法的用法:
示例1:

// Java code to illustrate toLongArray() 
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("BitSet: " + init_bitset); 
  
        long[] arr = init_bitset.toLongArray(); 
        System.out.println("The LongArray is: " + arr); 
  
        // Displaying the byteArray 
        System.out.println("The elements in the LongArray:"); 
        for (int i = 0; i < arr.length; i++) 
            System.out.print(arr[i] + ", "); 
    } 
}
輸出:
BitSet: {10, 20, 30, 40, 50}
The LongArray is: [J@232204a1
The elements in the LongArray:
1127000493261824,

示例2:

// Java code to illustrate toLongArray() 
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(48); 
        init_bitset.set(64); 
        init_bitset.set(15); 
        init_bitset.set(95); 
        init_bitset.set(105); 
        init_bitset.set(21); 
  
        // Displaying the BitSet 
        System.out.println("BitSet: " + init_bitset); 
  
        long[] arr = init_bitset.toLongArray(); 
        System.out.println("The LongArray is: " + arr); 
  
        // Displaying the byteArray 
        System.out.println("The elements in the LongArray:"); 
        for (int i = 0; i < arr.length; i++) 
            System.out.print(arr[i] + ", "); 
    } 
}
輸出:
BitSet: {15, 21, 48, 64, 95, 105}
The LongArray is: [J@232204a1
The elements in the LongArray:
281474978840576, 2201170739201,


相關用法


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