有三clear()方法的變體:
- clear():clear()方法將此BitSet中的所有位設置為false。
public void clear() Return Value This method does not return a value.
// Java code to demonstrate the working // of clear() in BitSet import java.util.*; public class BitClr1 { public static void main(String[] args) { // Declaring Bitset BitSet bset = new BitSet(8); // assigning values to bitset for (int i = 0; i < 5; i++) bset.set(i); // printing original bitset System.out.println ("The bitset before clear() operation is:" + bset); // using clear() to clear contents of bitset bset.clear(); // printing bitset after clear() operation // empty bitset System.out.println ("The bitset after clear() operation is:" + bset); } }
輸出:
The bitset before clear() operation is:{0, 1, 2, 3, 4} The bitset after clear() operation is:{}
- clear(int pos):clear(int pos)方法將索引指定的位設置為false。即從位集中刪除。
public void clear(int pos) 參數 pos: the index of the bit to be cleared. Return Value This method does not return a value. Exception IndexOutOfBoundsException: if the specified index is negative.
// Java code to demonstrate the working // of clear(int pos) in BitSet import java.util.*; public class BitClr2 { public static void main(String[] args) { // Declaring Bitset BitSet bset = new BitSet(8); // assigning values to bitset for (int i = 0; i < 5; i++) bset.set(i); // printing original bitset System.out.println ("The bitset before clear(pos) operation is:" + bset); // using clear(pos) to clear element at specified position bset.clear(3); // printing bitset after clear(pos) operation // removes 3 System.out.println ("The bitset after clear(pos) operation is:" + bset); } }
輸出:
The bitset before clear(pos) operation is:{0, 1, 2, 3, 4} The bitset after clear(pos) operation is:{0, 1, 2, 4}
- clear(int frompos,int topos):clear(int frompos,int topos)方法將指定的frompos(包括)到指定的topos(不包括)的位設置為false,即在一定範圍內執行刪除操作。
public void clear(int frompos, int topos) 參數 frompos: index of the first bit to be cleared topos: index of the last bit to be cleared Return Value This method does not return a value. Exception IndexOutOfBoundsException: if frompos is negative, or topos is negative, or frompos is larger than topos.
// Java code to demonstrate the working // of clear(int frompos, int topos) in BitSet import java.util.*; public class BitClr3 { public static void main(String[] args) { // Declaring Bitset BitSet bset = new BitSet(8); // assigning values to bitset for (int i = 0; i < 5; i++) bset.set(i); // printing original bitset System.out.println ("The bitset before clear(frompos, topos) operation is:" + bset); // using clear(frompos, topos) to clear elements in range bset.clear(2, 4); // printing bitset after clear(frompos, topos) operation // removes 2, 3 System.out.println ("The bitset after clear(frompos, topos) operation is:" + bset); } }
輸出:
The bitset before clear(frompos, topos) operation is:{0, 1, 2, 3, 4} The bitset after clear(frompos, topos) operation is:{0, 1, 4}
相關用法
- Java Java.util.function.IntPredicate用法及代碼示例
- Java Java lang.Long.numberOfTrailingZeros()用法及代碼示例
- Java Java.util.concurrent.Phaser用法及代碼示例
- Java Java lang.Long.reverse()用法及代碼示例
- Java Java lang.Long.lowestOneBit()用法及代碼示例
- Java Java lang.Long.byteValue()用法及代碼示例
- Java Java lang.Long.builtcount()用法及代碼示例
- Java Java lang.Long.highestOneBit()用法及代碼示例
- Java Java.util.function.LongPredicate用法及代碼示例
- Java Java.util.function.DoublePredicate用法及代碼示例
- Java Java.util.function.BiPredicate用法及代碼示例
- Java Java.util.concurrent.RecursiveAction用法及代碼示例
注:本文由純淨天空篩選整理自 Java.util.BitSet.clear() in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。