Java中的Java .util.BitSet.get()
Bitset 中的 get() 有两种变体,本文将讨论这两种变体。
1. boolean get( int value ) :如果值存在于 Bitset 中,则返回 true,否则返回 false。
Declaration :
public boolean get(int value)
Parameters :
value : The value to check.
Return Value :
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,该BitSet由Bitset中存在的元素组成,从fromvale(包括)到toval(不包括)。
Declaration :
public BitSet get(int fromval, int toval)
Parameters :
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}