1 回答

TA貢獻(xiàn)1821條經(jīng)驗(yàn) 獲得超6個(gè)贊
這是 BitSet.length 的文檔:
length()
Returns the "logical size" of this BitSet: the index of the highest set bit in the BitSet plus one.
如果您需要打印出一定數(shù)量的位(例如 60),則在循環(huán)中使用常量而不是“.length()”。無論長度如何,您都可以在任何索引上調(diào)用“.get(index)”,它將為您提供該位的結(jié)果。
例如下面的代碼產(chǎn)生“0000011000”:
import java.util.BitSet;
public class Main {
public static void main(String[] args) {
BitSet bits = new BitSet();
bits.set(5);
bits.set(6);
StringBuilder bitString = new StringBuilder();
for (int i = 0; i < 10; i++) {
bitString.append(bits.get(i) ? "1" : "0");
}
System.out.println(bitString.toString());
}
}
添加回答
舉報(bào)