![](/img/trans.png)
[英]Why does Java's BitSet internally store long array but use int for set method?
[英]Why BitSet class and associated set method is used for?
為什么我們在何時實時使用java.util.BitSet
類。 set()
方法有什么作用? Java Doc
造成了一些混亂。 有人可以澄清嗎?
提前致謝
java.util.BitSet.and(BitSet set)方法對該目標位集與參數位集執行邏輯與。 修改此位集,以便當且僅當它最初都具有值true且位集參數中的相應位也具有值true時,其中的每個位才具有true值。
public static void main(String[] args) {
// create 2 bitsets
BitSet bitset1 = new BitSet(8);
BitSet bitset2 = new BitSet(8);
// assign values to bitset1
bitset1.set(0);
bitset1.set(1);
bitset1.set(2);
bitset1.set(3);
bitset1.set(4);
bitset1.set(5);
// assign values to bitset2
bitset2.set(2);
bitset2.set(4);
bitset2.set(6);
bitset2.set(8);
bitset2.set(10);
// print the sets
System.out.println("Bitset1:" + bitset1);
System.out.println("Bitset2:" + bitset2);
// perform and operation between two bitsets
bitset1.and(bitset2);
// print the new bitset1
System.out.println("" + bitset1);
}
輸出: -
Bitset1:{0, 1, 2, 3, 4, 5}
Bitset2:{2, 4, 6, 8, 10}
{2, 4}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.