編程-Java學習- BitSet.cardinality()的一個小技巧

        首先我們知道BitSet.cardinary()是判斷bitset中有一個設置爲1的數量。但是如果我們想知道到底是那幾個被設置爲1了該怎麼做?

// 從0開始往後依次尋找
for(int i =0 ; i< bitset.cardinary();i++){
        //找到上面的i出現的位置
        //This method in BitSet Class is used to return the index of the first bit 
        //that is set to true, that occurs on or after the specified starting index
        System.out.println(BitSet.nextBit(i));
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章