有三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 lang.Long.numberOfTrailingZeros()用法及代码示例
- 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.util.BitSet.clear() in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。