验证 BitSet 是否为空
当 BitSet 中的所有值都为假时,它被认为是空的。BitSet 类提供了 isEmpty() 方法。此方法返回一个布尔值,当当前 BitSet 为空时返回 false,不为空时返回 true。
您可以使用 isEmpty() 方法验证特定的 BitSet 是否为空。
示例
import java.util.BitSet;
public class isEmpty {
public static void main(String args[]) {
BitSet bitSet = new BitSet(10);
for (int i = 1; i<25; i++) {
if(i%2==0) {
bitSet.set(i);
}
}
if (bitSet.isEmpty()) {
System.out.println("This BitSet is empty");
} else {
System.out.println("This BitSet is not empty");
System.out.println("The contents of it are : "+bitSet);
}
bitSet.clear();
if (bitSet.isEmpty()) {
System.out.println("This BitSet is empty");
} else {
System.out.println("This BitSet is not empty");
System.out.println("The contents of it are : "+bitSet);
}
}
}
输出
This BitSet is not empty
The contents of it are : {2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24}
This BitSet is empty
广告