Java中的 BitSet length() 方法及示例
Java中BitSet类的length()方法用于知道这个BitSet的逻辑大小。这个逻辑大小等于 BitSet 的最高位加一。
句法:
BitSet.hashCode()
参数:该方法不接受任何参数。
返回值:该方法返回BitSet的逻辑大小,等于集合的最高位加一。如果 BitSet 为空,则该方法返回零。
以下程序用于说明 BitSet.length() 方法的工作原理:
方案一:
// Java code to illustrate length()
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(31);
init_bitset.set(100);
init_bitset.set(53);
// Displaying the BitSet
System.out.println("BitSet: " + init_bitset);
// Displaying the length or logical size
System.out.println("The Length is: "
+ init_bitset.length());
}
}
输出:
BitSet: {25, 31, 40, 53, 100}
The Length is: 101
方案二:
// Java code to illustrate length()
import java.util.*;
public class BitSet_Demo {
public static void main(String args[])
{
// Creating an empty BitSet
BitSet init_bitset = new BitSet();
// Displaying the BitSet
System.out.println("BitSet: " + init_bitset);
// Displaying the hashcode
System.out.println("The Length is: "
+ init_bitset.length());
}
}
输出:
BitSet: {}
The Length is: 0