當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。