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

后端 未结 6 1401
借酒劲吻你
借酒劲吻你 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 02:00

    This puzzled me too, not sure of the rationale behind BitSet's current rather unexpected functionality. However since it's not final, we can use some embrace and extend tactics and do the following to get a fixed BitSet with length semantics as expected:

    import java.util.BitSet;
    
    /**
     * Variation of BitSet which does NOT interpret the highest bit synonymous with
     * its length.
     *
     * @author casper.bang@gmail.com
     */
    public class FixedBitSet extends BitSet{
    
        int fixedLength;
    
        public FixedBitSet(int fixedLength){
            super(fixedLength);
            this.fixedLength = fixedLength;
        }
    
        @Override
        public int length() {
            return fixedLength;
        }
    }
    

提交回复
热议问题