java.util.BitSet中 - 集()如预期不起作用 [英] java.util.BitSet -- set() doesn't work as expected
问题描述
我缺少的东西非常明显?抑或只是在世界上没有人实际使用java.util.BitSet中?
下面的测试失败:
@Test
公共无效testBitSet()抛出异常{
位集合B =新的BitSet();
b.set(0,真);
b.set(1,FALSE);
的assertEquals(2,b.length个());
}
这真是我不清楚我为什么不跟长度为2的位集合和值10我在源java.util.BitSet中偷看结束,并在临时检查似乎未能作出足够的区分从未被设置为任意值一个已经设置为false了一下,有点之间...
(注意,明确设置位集合的大小,在构造函数没有任何效果的,例如:
位集合B =新的BitSet(2);
人们不使用位集合
;但是,他们用它比你打算什么以外的东西。这也可能是最好把位集合
为设置℃的非常紧凑,内存的有效形式;整数GT;
具有你不能把特殊性质负数进去。
这是非常常见的有位集合
S IN的方式使用它们。
的for(int n = set.nextSetBit(0); ID> = 0; n = set.nextSetBit(ID + 1)){
//做的东西到一组索引
}
你做的东西来填补起来了。这相当于迭代的元素设置
。
Am I missing something painfully obvious? Or does just nobody in the world actually use java.util.BitSet?
The following test fails:
@Test
public void testBitSet() throws Exception {
BitSet b = new BitSet();
b.set(0, true);
b.set(1, false);
assertEquals(2, b.length());
}
It's really unclear to me why I don't end up with a BitSet of length 2 and the value 10. I peeked at the source for java.util.BitSet, and on casual inspection it seems to fail to make sufficient distinction between a bit that's been set false and a bit that has never been set to any value...
(Note that explicitly setting the size of the BitSet in the constructor has no effect, e.g.:
BitSet b = new BitSet(2);
People do use BitSet
; however, they use it for something other than what you intend. It's probably best to think of BitSet
as a very compact, memory-efficient form of Set<Integer>
that has the peculiar property that you can't put negative numbers into it.
It's very common with BitSet
s to use them in the pattern of
for (int id = set.nextSetBit(0); id >= 0; id = set.nextSetBit(id + 1)) {
// do stuff to a set index
}
after you do something to fill them up. This is equivalent to iterating over the elements of the Set
.
这篇关于java.util.BitSet中 - 集()如预期不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!