列印 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);
廣告