打印 BitSet 元素
BitSet 类的 get() 方法返回指定索引处比特的当前状态/值。使用它可以打印 BitSet 的内容。
示例
import java.util.BitSet;
public class PrintingElements {
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);
}
}
System.out.println("After clearing the contents ::");
for (int i = 1; i<=25; i++) {
System.out.println(i+": "+bitSet.get(i));
}
}
}
输出
After clearing the contents :: 1: false 2: true 3: false 4: true 5: false 6: true 7: false 8: true 9: false 10: true 11: false 12: true 13: false 14: true 15: false 16: true 17: false 18: true 19: false 20: true 21: false 22: true 23: false 24: true 25: false
或者,您可以使用 println() 方法直接打印比特集的内容。
System.out.println(bitSet);
广告