当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Java.util.BitSet.set()用法及代码示例


set()方法有四种变体。本文介绍了所有这些变体,如下所示:

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

Declaration:
public void set(int bitIndex)
参数:
     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):此方法将指定索引处的位设置为指定值,如果布尔值为true,则添加值,否则不添加。

Declaration:
  public void set(int num, boolean value)
参数:
   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(包括)到指定的tonum(不包括)的位设置为true,即,将值fromnum添加到tonum-1。

Declaration:
   public void set(int fromnum, int tonum)
参数:
    fromnum: value to start insert.
    tonum: value to end insertion.
返回值:
   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):此方法将指定fromfromnum(包括)到指定tonum(不包括)的位设置为指定值,即,如果传递的布尔值为true,则将fromnum插入tonum-1。 ,否则不会插入。

Declaration:
  public void set(int fromnum, int tonum, boolean value)
参数:
   fromnum: num to start inserting.
   tonum: last number of insertion.
   value: value to set the selected bits to.
返回值:
   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}


相关用法


注:本文由纯净天空筛选整理自 Java.util.BitSet.set() method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。