There are three variants of clear() method:
- clear() : The clear() method sets all of the bits in this BitSet to false.
public void clear() Return Value This method does not return a value.
// Java code to demonstrate the working// of clear() in BitSetimportjava.util.*;publicclassBitClr1 {publicstaticvoidmain(String[] args)   Â{       Â// Declaring Bitset       ÂBitSet bset =newBitSet(8);       Â// assigning values to bitset       Âfor(inti =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);   Â}}Output:
The bitset before clear() operation is : {0, 1, 2, 3, 4} The bitset after clear() operation is : {} - clear(int pos) : The clear(int pos) method sets the bit specified by the index to false. i.e removes from bitset.
public void clear(int pos) Parameters 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 BitSetimportjava.util.*;publicclassBitClr2 {publicstaticvoidmain(String[] args)   Â{       Â// Declaring Bitset       ÂBitSet bset =newBitSet(8);       Â// assigning values to bitset       Âfor(inti =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);   Â}}Output:
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) : The clear(int frompos, int topos) method sets the bits from the specified frompos (inclusive) to the specified topos (exclusive) to false i.e performs removal in a range.
public void clear(int frompos, int topos) Parameters 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 BitSetimportjava.util.*;publicclassBitClr3 {publicstaticvoidmain(String[] args)   Â{       Â// Declaring Bitset       ÂBitSet bset =newBitSet(8);       Â// assigning values to bitset       Âfor(inti =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);   Â}}Output:
The bitset before clear(frompos, topos) operation is : {0, 1, 2, 3, 4} The bitset after clear(frompos, topos) operation is : {0, 1, 4}
This article is contributed by Astha Tyagi. If you like Lazyroar and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the Lazyroar main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
