1）使用整数数组来替代；

2）使用BitSet；

BitSet实际是由“二进制位”构成的一个Vector。如果希望高效率地保存大量“开－关”信息，就应使用BitSet。它只有从尺寸的角度看才有意义；如果希望的高效率的访问，那么它的速度会比使用一些固有类型的数组慢一些。

BitSet的大小与实际申请的大小并不一定一样，BitSet的size方法打印出的大小一定是64的倍数，这与它的实际申请代码有关，假设以下面的代码实例化一个BitSet:

``BitSet set =            new            BitSet(           129           );``

 ``````/** * Creates a bit set whose initial size is large enough to explicitly * represent bits with indices in the range 0 through * nbits-1. All bits are initially false. * * @param nbits the initial size of the bit set. * @exception NegativeArraySizeException if the specified initial size * is negative. */ public BitSet( int nbits) { // nbits can't be negative; size 0 is OK if (nbits < 0 ) throw new NegativeArraySizeException( "nbits < 0: " + nbits); initWords(nbits); sizeIsSticky = true ; } private void initWords( int nbits) { words = new long [wordIndex(nbits- 1 ) + 1 ]; }``````

 ``````/** * Given a bit index, return word index containing it. */ private static int wordIndex( int bitIndex) { return bitIndex >> ADDRESS_BITS_PER_WORD; }``````

 ``private final static int ADDRESS_BITS_PER_WORD = 6 ;``

1）set方法，源码如下：

 ``````/** * Sets the bit at the specified index to true. * * @param bitIndex a bit index. * @exception IndexOutOfBoundsException if the specified index is negative. * @since JDK1.0 */ public void set( int bitIndex) { if (bitIndex < 0 ) throw new IndexOutOfBoundsException( "bitIndex < 0: " + bitIndex); int wordIndex = wordIndex(bitIndex); expandTo(wordIndex); words[wordIndex] |= (1L << bitIndex); // Restores invariants checkInvariants(); }``````

 ``words[wordIndex] |= (1L << bitIndex); // Restores invariants``

 ``````int transderBits = bitIndex % 64 ; words[wordsIndex] |= (1L << transferBits);``````

 ``````/** * Sets the bit specified by the index to false. * * @param bitIndex the index of the bit to be cleared. * @exception IndexOutOfBoundsException if the specified index is negative. * @since JDK1.0 */ public void clear( int bitIndex) { if (bitIndex < 0 ) throw new IndexOutOfBoundsException( "bitIndex < 0: " + bitIndex); int wordIndex = wordIndex(bitIndex); if (wordIndex >= wordsInUse) return ; words[wordIndex] &= ~(1L << bitIndex); recalculateWordsInUse(); checkInvariants(); }``````

expandTo方法：

 ``````/** * Ensures that the BitSet can accommodate a given wordIndex, * temporarily violating the invariants. The caller must * restore the invariants before returning to the user, * possibly using recalculateWordsInUse(). * @param wordIndex the index to be accommodated. */ private void expandTo( int wordIndex) { int wordsRequired = wordIndex+ 1 ; if (wordsInUse < wordsRequired) { ensureCapacity(wordsRequired); wordsInUse = wordsRequired; } }``````

 ``````/** * The number of words in the logical size of this BitSet. */ private transient int wordsInUse = 0 ;``````

 ``````private void ensureCapacity( int wordsRequired) { if (words.length < wordsRequired) { // Allocate larger of doubled size or required size int request = Math.max( 2 * words.length, wordsRequired); words = Arrays.copyOf(words, request); sizeIsSticky = false ; } }``````

 ``````/** * Whether the size of "words" is user-specified. If so, we assume * the user knows what he's doing and try harder to preserve it. */ private transient boolean sizeIsSticky = false ;``````

2）get方法：

 ``````/** * Returns the value of the bit with the specified index. The value * is true if the bit with the index bitIndex * is currently set in this BitSet; otherwise, the result * is false. * * @param bitIndex the bit index. * @return the value of the bit with the specified index. * @exception IndexOutOfBoundsException if the specified index is negative. */ public boolean get( int bitIndex) { if (bitIndex < 0 ) throw new IndexOutOfBoundsException( "bitIndex < 0: " + bitIndex); checkInvariants(); int wordIndex = wordIndex(bitIndex); return (wordIndex < wordsInUse) && ((words[wordIndex] & (1L << bitIndex)) != 0 ); }``````

 ``return (wordIndex < wordsInUse) && ((words[wordIndex] & (1L << bitIndex)) != 0 );``

3）size()方法：

 ``````/** * Returns the number of bits of space actually in use by this * BitSet to represent bit values. * The maximum element in the set is the size - 1st element. * * @return the number of bits currently in this bit set. */ public int size() { return words.length * BITS_PER_WORD; }``````

 ``````private final static int ADDRESS_BITS_PER_WORD = 6 ; private final static int BITS_PER_WORD = 1 << ADDRESS_BITS_PER_WORD;``````

4）与size相似的一个方法：length()源码如下：

 ``````/** * Returns the "logical size" of this BitSet: the index of * the highest set bit in the BitSet plus one. Returns zero * if the BitSet contains no set bits. * * @return the logical size of this BitSet. * @since 1.2 */ public int length() { if (wordsInUse == 0 ) return 0 ; return BITS_PER_WORD * (wordsInUse - 1 ) + (BITS_PER_WORD - Long.numberOfLeadingZeros(words[wordsInUse - 1 ])); }``````

 ``long test = 1 ;
System.out.println(Long.numberOfLeadingZeros(test<< 40 | test<< 4 ));``

60
23
23