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 Java lang.Long.numberOfLeadingZeros()用法及代碼示例
- Java Java.util.Collections.disjoint()用法及代碼示例
- Java Java lang.Long.highestOneBit()用法及代碼示例
- Java Java.util.Collections.rotate()用法及代碼示例
- Java Java lang.Long.numberOfTrailingZeros()用法及代碼示例
- Java Java lang.Long.byteValue()用法及代碼示例
- Java Java lang.Long.reverse()用法及代碼示例
- Java Java lang.Long.lowestOneBit()用法及代碼示例
- Java Java.util.ArrayList.addall()用法及代碼示例
- Java Clock withZone()用法及代碼示例
- Java Clock tickMinutes()用法及代碼示例
- Java Java.math.BigInteger.probablePrime()用法及代碼示例
- Java Java.math.BigInteger.modInverse()用法及代碼示例
注:本文由純淨天空篩選整理自 Java.util.BitSet.set() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。