当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。