(1)BitSet类 默认情况下,set 中所有位的初始值都是 false。 每个位 set 都有一个当前大小,也就是该位 set 当前所用空间的位数。注意,这个大小与位 set 的实现有关,所以它可能随实现的不同而更改。位 set 的长度与位 set 的逻辑长度有关,并且是与实现无关而定义的。 除非另行说明,否则将 null 参数传递给 BitSet 中的任何方法都将导致 NullPointerException。 在没有外部同步的情况下,多个线程操作一个 BitSet 是不安全的。 |
|
(2) 构造函数: BitSet() or BitSet(int nbits)
(3) 一些方法
public void set(int pos): 位置pos的字位设置为true。
public void set(int bitIndex, boolean value) 将指定索引处的位设置为指定的值。
public void clear(int pos): 位置pos的字位设置为false。
public void clear() : 将此 BitSet 中的所有位设置为 false。
public int cardinality() 返回此 BitSet 中设置为 true 的位数。
public boolean get(int pos): 返回位置是pos的字位值。
public void and(BitSet other): other同该字位集进行与操作,结果作为该字位集的新值。
public void or(BitSet other): other同该字位集进行或操作,结果作为该字位集的新值。
public void xor(BitSet other): other同该字位集进行异或操作,结果作为该字位集的新值。
public void andNot(BitSet set) 清除此 BitSet 中所有的位,set - 用来屏蔽此 BitSet 的 BitSet
public int size(): 返回此 BitSet 表示位值时实际使用空间的位数。
public int length() 返回此 BitSet 的“逻辑大小”:BitSet 中最高设置位的索引加 1。
public int hashCode(): 返回该集合Hash 码, 这个码同集合中的字位值有关。
public boolean equals(Object other): 如果other中的字位同集合中的字位相同,返回true。
public Object clone() 克隆此 BitSet,生成一个与之相等的新 BitSet。
public String toString() 返回此位 set 的字符串表示形式。
import java.util.BitSet; public class WhichChars{ private BitSet used = new BitSet(); public WhichChars(String str){ for(int i=0;i< str.length();i++) used.set(str.charAt(i)); // set bit for char } public String toString(){ String desc="["; int size=used.size(); for(int i=0;i< size;i++){ if(used.get(i)) desc+=(char)i; } return desc+"]"; } public static void main(String args[]){ WhichChars w=new WhichChars("How do you do"); System.out.println(w); } }运行:
C:\work>java WhichChars
[ Hdouwy]