📜  Java中的Java .util.BitSet.set() 方法

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

Java中的Java .util.BitSet.set() 方法

set() 方法有四种变体。本文描述了所有变体,如下所示:

1. set(int Index) :此方法将指定索引处的位设置为true,即添加一个值。

Declaration : 
public void set(int bitIndex)
Parameters : 
     Index :  a bit index.
Result : 
  This method does not return a value.
Exception : 
     IndexOutOfBoundsException :  if the specified index is negative.
// Java code to demonstrate the working
// of set() in BitSet
import java.util.*;
public class BitSet1 {
public static void main(String[] args)
    {
        // creating two bitsets
        BitSet bset = new BitSet(5);
  
        // assigning value using set()
        bset.set(3);
        bset.set(5);
  
        // printing the bitset
        System.out.println("The constructed bitset is : " + bset);
    }
}

输出:

The constructed bitset is : {3, 5}

2. set(int num, boolean value) :该方法将指定索引处的位设置为指定值,如果布尔值为真,则添加值,否则不添加。

Declaration : 
  public void set(int num, boolean value)
Parameters : 
   num :  a bit value.
   value :  a boolean value to set.
Return Value
    This method does not return a value.
// Java code to demonstrate the working
// of set(num, value) in BitSet
import java.util.*;
public class BitSet2 {
public static void main(String[] args)
    {
        // creating two bitsets
        BitSet bset = new BitSet(5);
  
        // assigning value using set(num, value)
        // adds 3
        bset.set(3, true);
  
        // doesn't add 5
        bset.set(5, false);
        bset.set(7, true);
        bset.set(4, false);
  
        // printing the bitset
        System.out.println("The constructed bitset is : " + bset);
    }
}

输出:

The constructed bitset is : {3, 7}

3. set(int fromnum, int tonum) :该方法设置从指定fromnum(inclusive)到指定tonum(exclusive)的位为true,即将值fromnum加到tonum-1

Declaration : 
   public void set(int fromnum, int tonum)
Parameters : 
    fromnum :  value to start insert.
    tonum :  value to end insertion.
Return Value : 
   This method does not return a value.
// Java code to demonstrate the working
// of set(fromnum, tonum) in BitSet
import java.util.*;
public class BitSet3 {
public static void main(String[] args)
    {
        // creating two bitsets
        BitSet bset = new BitSet(9);
  
        // assigning value using set(fromnum, tonum)
        // adds 3 to 8
        bset.set(3, 9);
  
        // printing the bitset
        System.out.println("The constructed bitset is : " + bset);
    }
}

输出:

The constructed bitset is : {3, 4, 5, 6, 7, 8}

4. set(int fromnum, int tonum, boolean value) :该方法将指定fromnum(包括)到指定tonum(不包括)的位设置为指定值,即如果传递的布尔值为true ,则将fromnum插入tonum-1 , 否则不插入.

Declaration : 
  public void set(int fromnum, int tonum, boolean value)
Parameters : 
   fromnum :  num to start inserting.
   tonum :  last number of insertion.
   value :  value to set the selected bits to.
Return Value : 
   This method does not return a value.
// Java code to demonstrate the working
// of set(fromnum, tonum, value) in BitSet
import java.util.*;
public class BitSet4 {
public static void main(String[] args)
    {
        // creating two bitsets
        BitSet bset = new BitSet(9);
  
        // assigning value using set(fromnum, tonum, value)
        // doesn't adds 3 to 8
        bset.set(3, 9, false);
  
        // assigning value using set(fromnum, tonum, value)
        // adds 5 to 10
        bset.set(5, 11, true);
  
        // printing the bitset
        System.out.println("The constructed bitset is : " + bset);
    }
}

输出:

The constructed bitset is : {5, 6, 7, 8, 9, 10}