BitSet是java中定义的一个类。util包。它创建一个由布尔值表示的位数组。
null
先决条件: Java位集|集1
nextClearBit()方法: BitSet类中的此方法用于返回在指定起始索引时或之后设置为false的第一位的索引。
语法:
public int nextClearBit(int fromIndex)
参数: 此方法采用强制参数 fromIndex 这是开始检查下一个错误位的索引(包括)。
返回值: 此方法返回下一个清除位的索引,即在指定的起始索引上或之后出现的设置为false的第一个位的索引。
例外情况: 这个方法抛出 IndexOutOfBoundsException 如果指定的索引为负。
例1: 要显示nextClearBit()函数的实现:
// Java program illustrating Bitset // nextClearBit() 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(); /* 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); // Print the first clear bit of bs1 System.out.println(bs1.nextClearBit( 0 )); // Print the first clear bit of bs2 after index 3 System.out.println(bs2.nextClearBit( 3 )); } } |
输出:
bs1 : {0, 1, 2, 4} bs2 : {1, 2, 3, 4, 5, 6, 12} 3 7
例2: 要显示IndexOutOfBoundException,请执行以下操作:
// Java program illustrating Bitset // nextClearBit() 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(); /* 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); try { // Passing -1 as parameter System.out.println(bs1.nextClearBit(- 1 )); } catch (Exception e) { System.out.println( "Exception when " + "negative index is passed " + "as parameter : " + e); } } } |
输出:
bs1 : {0, 1, 2, 4} bs2 : {1, 2, 3, 4, 5, 6, 12} Exception when negative index is passed as parameter : java.lang.IndexOutOfBoundsException: fromIndex < 0: -1
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END