首先给出BitSet的官方说明:
This class implements a vector of bits that grows as needed. Each component of the bit set has a boolean value. The bits of a BitSet are indexed by nonnegative integers. Individual indexed bits can be examined, set, or cleared. One BitSet may be used to modify the contents of another BitSet through logical AND, logical inclusive OR, and logical exclusive OR operations.
By default, all bits in the set initially have the value false.
Every bit set has a current size, which is the number of bits of space currently in use by the bit set. Note that the size is related to the implementation of a bit set, so it may change with implementation. The length of a bit set relates to logical length of a bit set and is defined independently of implementation.
Unless otherwise noted, passing a null parameter to any of the methods in a BitSet will result in a NullPointerException.
A BitSet is not safe for multithreaded use without external synchronization.
简单的说就是:
- BitSet提供了一个动态的根据需要增加的位向量
- 该位向量每一位的值为布尔值,且初始时都为false
- 可以通过:bitSet.set(index) 将某一位设为true,该方法不接受null参数
- BitSet为非同步的
另外
- 可以通过 bitSet.get(index) 来获取某一位的状态
- 可以通过
for (int bit = sum.length(); (bit = sum.previousSetBit(bit-1)) >= 0;) {}方法遍历所有为 true 的位,bit值即为索引值
Java BitSet详解:动态位向量操作与使用
Java的BitSet是一个动态增长的位向量,用于存储布尔值,初始全为false。通过BitSet.set(index)可以设置指定位置为true,get(index)检查状态。遍历true位使用for循环结合previousSetBit方法。注意BitSet不是线程安全的,多线程使用需外部同步。
4397

被折叠的 条评论
为什么被折叠?



