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


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


Bitset中有get()的两种变体,本文都对此进行了讨论。

1. boolean get(int value):如果该值存在于Bitset中,则返回true;否则返回false。

Declaration:
public boolean get(int value)
参数:
value: The value to check.
返回值:
Returns boolean true, if element present else returns false.
// Java code to demonstrate the 
// working of get() in Bitset 
  
import java.util.*; 
  
public class BitGet1 { 
  
public static void main(String[] args) 
    { 
  
        // declaring bitset 
        BitSet bset = new BitSet(5); 
  
        // adding values using set() 
        bset.set(0); 
        bset.set(1); 
        bset.set(2); 
        bset.set(4); 
  
        // checking if 3 is in BitSet 
        System.out.println("Does 3 exist in Bitset?:" + bset.get(3)); 
  
        // checking if 4 is in BitSet 
        System.out.println("Does 4 exist in Bitset?:" + bset.get(4)); 
    } 
}

输出:


Does 3 exist in Bitset?:false
Does 4 exist in Bitset?:true

2. Bitset get(int fromval,int toval):方法返回一个新的BitSet,它由Bitval中存在的元素组成,从fromval(包括)到toval(不包括)。

Declaration:
public BitSet get(int fromval, int toval)
参数:

fromval: first value to include.
toval:last value to include(ex).

Return Value
This method returns a new BitSet from a range of this BitSet.
// Java code to demonstrate the 
// working of get(int fromval, int toval) 
// in Bitset 
  
import java.util.*; 
  
public class BitGet2 { 
  
public static void main(String[] args) 
    { 
  
        // declaring bitset 
        BitSet bset = new BitSet(5); 
  
        // adding values using set() 
        bset.set(0); 
        bset.set(1); 
        bset.set(2); 
        bset.set(3); 
  
        // Printing values in range 0-2 
        System.out.println("Values in BitSet from 0-2 are:" + bset.get(0, 3)); 
    } 
}

输出:

Values in BitSet from 0-2 are:{0, 1, 2}


相关用法


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