位集合(BitSet)| Java数据结构
BitSet使用示例:
import java.util.BitSet; public class BitSetDemo { public static void main(String args[]) { BitSet bits1 = new BitSet(16); BitSet bits2 = new BitSet(16); // set some bits for (int i = 0; i < 16; i++) { if ((i % 2) == 0) { bits1.set(i); } if ((i % 5) != 0) { bits2.set(i); } } System.out.println(bits1); System.out.println(bits2); System.out.println("--------------------"); //void and(BitSet set) /*bits2.and(bits1); System.out.println(bits1); System.out.println(bits2);*/ //void andNot(BitSet set) /*bits2.andNot(bits1); System.out.println(bits1); System.out.println(bits2);*/ //int cardinality( ) /*System.out.println(bits1.cardinality()); System.out.println(bits2.cardinality());*/ //void clear( ) /*bits1.clear(); System.out.println(bits1); System.out.println(bits2);*/ // void clear(int index) /*bits1.clear(2); System.out.println(bits1); System.out.println(bits2);*/ // void clear(int startIndex, int endIndex) /*bits1.clear(2,6);//前开后闭 System.out.println(bits1); System.out.println(bits2);*/ // Object clone( ) //boolean equals(Object bitSet) // BitSet bits3 = (BitSet) bits1.clone(); // System.out.println(bits3); // System.out.println(bits3.equals(bits1)); //void flip(int index) /*bits1.flip(0); bits1.flip(2); bits1.flip(3); System.out.println(bits1); System.out.println(bits2);*/ //boolean get(int index) // System.out.println(bits1.get(4)); // System.out.println(bits1.get(5)); //BitSet get(int startIndex, int endIndex) // BitSet bits3=bits1.get(2,6); // System.out.println(bits3); //int hashCode( ) // System.out.println(bits1.hashCode()); // System.out.println(bits1.hashCode()); //boolean intersects(BitSet bitSet) /*BitSet bits3 = new BitSet(16); System.out.println(bits3); bits3.set(0); System.out.println(bits3.intersects(bits1)); System.out.println(bits3.intersects(bits2)); System.out.println(bits1); System.out.println(bits2); System.out.println(bits3);*/ //boolean isEmpty( ) // bits1.clear(); // System.out.println(bits1); // System.out.println(bits1.isEmpty()); //... //OR bits // bits2.or(bits1); // System.out.println("bits2 OR bits1: "); // System.out.println(bits2); // // System.out.println(bits1.size()); // System.out.println(bits1.toString()); bits1.xor(bits2); System.out.println(bits1); } }