📜  Java.util.BitSet 类方法在Java中的示例|设置 2

📅  最后修改于: 2022-05-13 01:55:27.510000             🧑  作者: Mango

Java.util.BitSet 类方法在Java中的示例|设置 2

Methods discussed in this post:
                       BitSet class methods.
               /      /     |      |       \       \
           set()  xor()  clone() clear()  length()  cardinality()

我们强烈建议您参考下面的第 1 组作为先决条件。
Java中的BitSet类|设置 1

  1. set() : Java.util.BitSet.set()方法是将指定索引处的位设置为指定值。
    句法:
    public void set(int bitpos)
    public void set(int bitpos, boolean val)
    Parameters:
    bitpos : a bit index
    val : a boolean value to set
    Return: Nothing
    Throws: IndexOutOfBoundsException - if the specified index is negative
    
  2. clone() : Java.util.BitSet.clone()方法克隆一个 BitSet 生成一个与其相等的新 BitSet。位集的克隆是另一个位集,它具有与该位集完全相同的位设置为真。
    句法:
    public Object clone()
    Return: a clone of this bit set
    
  3. cardinality : Java.util.BitSet.cardinality()方法用于查找编号。 Bitset 中的元素。
    句法:
    public int cardinality()
    Return: the number of bits set to true in this BitSet.
    
    // Java program explaining BitSet class methods
    // set(), clone(), cardinality()
    import java.util.*;
    public class NewClasss
    {
        public static void main(String[] args)
        {
            BitSet bs1 = new BitSet();
            BitSet bs2 = new BitSet(8);
            BitSet bs3 = new BitSet();
      
            // assign values to bs1 using set()
            bs1.set(0);
            bs1.set(1);
            bs1.set(2);
            bs1.set(4);
      
            // assign values to bs2
            bs2.set(4);
            bs2.set(6);
            bs2.set(5);
      
            // Here we are using .clone() method to make
            // bs3 as bs1
            bs3 = (BitSet) bs1.clone();
      
            // Printing the 3 Bitsets
            System.out.println("bs1 : " + bs1);
            System.out.println("bs2 : " + bs2);
            System.out.println("bs3 cloned from bs1 : " + bs3);
      
            // Using .cardinality() method to print the no. of
            // elements of Bitset
            System.out.println("Cardinality of bs1 : " +
                                           bs1.cardinality());
            System.out.println("Cardinality of bs2 : " +
                                           bs2.cardinality());
        }
    }
    

    输出:

    bs1 : {0, 1, 2, 4}
    bs2 : {4, 5, 6}
    bs3 cloned from bs1 : {0, 1, 2, 4}
    Cardinality of bs1 : 4
    Cardinality of bs2 : 3
    
  4. clear() : Java.util.BitSet.clear()方法用于清除元素,即将所有Bitset 元素设置为false。
    句法:
    public void clear(int frompos,int topos)
    public void clear(int bitIndex)
    public void clear()
    Parameters:
    frompos - index of the first bit to be cleared
    topos - index after the last bit to be cleared
    bitIndex - the index of the bit to be cleared
    Throws:
    IndexOutOfBoundsException - if pos value is negative or frompos is larger than topos
    
  5. xor() : Java.util.BitSet.xor()方法对位集执行逻辑异或操作。
    此位集被修改,以便其中的位具有值 true 当且仅当:
    • 该位最初的值为 true,而参数中的相应位的值为 false。
    • 该位最初的值为 false,而参数中的相应位的值为 true。

    句法:

    public void xor(BitSet set)
    Parameters:
    set - the BitSet with which we need to perform operation
    
  6. length() : Java.util.BitSet.length()方法返回返回位集的逻辑大小。
    bitset 中最高设置位的索引加一。如果位集不包含设置位,则返回零。
    句法:
    public int length()
    Returns:
    the logical size of this BitSet
    
    // Java program explaining BitSet class methods
    //  xor(), length(), clear() methods
    import java.util.*;
    public class NewClass
    {
        public static void main(String[] args)
        {
            BitSet bs1 = new BitSet();
            BitSet bs2 = new BitSet();
      
            // assign values to bs1 using set()
            bs1.set(7);
            bs1.set(1);
            bs1.set(2);
            bs1.set(4);
            bs1.set(3);
            bs1.set(6);
      
            // assign values to bs2
            bs2.set(4);
            bs2.set(6);
            bs2.set(3);
            bs2.set(9);
            bs2.set(2);
      
            // Printing the Bitsets
            System.out.println("bs1 : " + bs1);
            System.out.println("bs2 : " + bs2);
      
            // use of length() method
            System.out.println("use of length() : " + bs1.length());
      
            // use of xor() to perform logical Xor operation
            bs1.xor(bs2);
            System.out.println("Use of xor() : " + bs1);
            bs2.xor(bs1);
            System.out.println("Use of xor() : " + bs2);
      
            // clear from index 2 to index 4 in bs1
            bs2.clear(1, 2);
            System.out.println("bs2 after clear method : " + bs2);
      
            // clear complete Bitset
            bs1.clear();
            System.out.println("bs1 after clear method : " + bs1);
        }
    }
    

    输出:

    bs1 : {1, 2, 3, 4, 6, 7}
    bs2 : {2, 3, 4, 6, 9}
    use of length() : 8
    Use of xor() : {1, 7, 9}
    Use of xor() : {1, 2, 3, 4, 6, 7}
    bs2 after clear method : {2, 3, 4, 6, 7}
    bs1 after clear method : {}
    


    Java中的BitSet 类方法与示例|设置 3

    参考 :
    https://docs.oracle.com/javase/7/docs/api/java Java