java.util.BitSet — set() doesn't work as expected

后端 未结 6 1361
借酒劲吻你
借酒劲吻你 2021-01-18 01:17

Am I missing something painfully obvious? Or does just nobody in the world actually use java.util.BitSet?

The following test fails:

@Test
public voi         


        
6条回答
  •  广开言路
    2021-01-18 01:55

    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 that has the peculiar property that you can't put negative numbers into it.

    It's very common with BitSets 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.

提交回复
热议问题