BitSet是java.util包中定義的類。它創建一個由布爾值表示的位數組。
先決條件:Java BitSet |套裝1
Bitset.previousClearBit()
此方法用於查找在作為參數傳遞的索引上或索引之前發生的錯誤位。此方法返回在指定的起始索引上或之前出現的,設置為false的最近位的索引。如果不存在這樣的位,或者給定-1作為起始索引,則返回-1。
用法:
public int previousClearBit(int fromIndex)
參數:此方法采用fromIndex,它是要從其開始檢查的索引(包括)。
返回值:此方法返回前一個清除位的索引,如果沒有該位,則返回-1
異常:如果指定的索引小於-1,則此方法將引發IndexOutOfBoundsException。
以下示例程序旨在說明previousCleatBit()方法:
示例1:
// Java program illustrating Bitset
// previousClearBit() function
import java.util.*;
public class GFG
{
public static void main(String[] args)
{
// Constructors of BitSet class
BitSet bs1 = new BitSet();
BitSet bs2 = new BitSet();
BitSet bs3 = new BitSet();
/* assigning values to set1*/
bs1.set(0);
//bs1.set(1);
bs1.set(2);
bs1.set(4);
// assign values to bs2
bs2.set(4);
bs2.set(6);
bs2.set(5);
bs2.set(1);
bs2.set(2);
bs2.set(3);
bs2.set(12);
// Printing the 2 Bitsets
System.out.println("bs1 : " + bs1);
System.out.println("bs2 : " + bs2);
System.out.println("bs3 : " + bs3);
// Performing previousClearBit() on bitsets
System.out.println("Previous Clear Bit of bs1"
+ bs1.previousClearBit(0));
System.out.println("Previous Clear Bit of bs2"
+ bs2.previousClearBit(5));
System.out.println("Previous Clear Bit of bs3"
+ bs3.previousClearBit(3));
}
}
輸出:
bs1 : {0, 2, 4} bs2 : {1, 2, 3, 4, 5, 6, 12} bs3 : {} Previous Clear Bit of bs1-1 Previous Clear Bit of bs20 Previous Clear Bit of bs33
示例2:要顯示IndexOutOfBoundException:
// Java program illustrating Bitset
// previousClearBit() function.
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Constructors of BitSet class
BitSet bs1 = new BitSet();
// assigning values to set1
bs1.set(0);
bs1.set(1);
bs1.set(2);
bs1.set(4);
// Printing the Bitset
System.out.println("bs1 : " + bs1);
try {
// Passing -2 as parameter
System.out.println(bs1.previousClearBit(-2));
}
catch (Exception e) {
System.out.println("Exception when "
+ "index less than -1 is passed "
+ "as parameter : " + e);
}
}
}
輸出:
bs1 : {0, 1, 2, 4} Exception when index less than -1 is passed as parameter : java.lang.IndexOutOfBoundsException: fromIndex
相關用法
- Java BitSet nextSetBit()用法及代碼示例
- Java BitSet isEmpty()用法及代碼示例
- Java BitSet nextClearBit()用法及代碼示例
- Java BitSet previousSetBit()用法及代碼示例
- Java BitSet stream()用法及代碼示例
- Java BitSet toByteArray()用法及代碼示例
- Java BitSet clone()用法及代碼示例
- Java BitSet hashCode用法及代碼示例
- Java BitSet length()用法及代碼示例
- Java BitSet toString()用法及代碼示例
- Java BitSet size()用法及代碼示例
- Java BitSet equals()用法及代碼示例
- Java BitSet toLongArray()用法及代碼示例
- Java Java.util.BitSet.set()用法及代碼示例
注:本文由純淨天空篩選整理自barykrg大神的英文原創作品 BitSet previousClearBit() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。