set(int, int, boolean): This method is available in java.util.BitSet class of Java.
Syntax:
void java.util.BitSet.set(int fromIndex, int toIndex, boolean value)
This method takes three arguments two are of type int and the other one of type boolean as its parameters. This method sets the bits from the specified fromIndex (inclusive) to the specified toIndex (exclusive) to the specified value.
Parameters: Three parameters are required for this method.
fromIndex: index of the first bit to be set.
toIndex: index after the last bit to be set.
value: value to set the selected bits too.
Throws:
IndexOutOfBoundsException - if fromIndex is negative,or toIndex is negative, or fromIndex is larger than toIndex.
Approach 1: When no exception
Java
import java.util.BitSet;public class BitSetset4 {public static void main(String[] args) {BitSet bitSet = new BitSet(100);bitSet.set(1);bitSet.set(50);bitSet.set(6);bitSet.set(10);int fromIndex = 2, toIndex = 10;bitSet.set(fromIndex, toIndex, false);System.out.println(bitSet);}}
Output:
{1, 10, 50}
Approach 2: IndexOutOfBoundsException
Java
import java.util.BitSet;public class BitSetset4 {public static void main(String[] args) {BitSet bitSet = new BitSet(100);bitSet.set(1);bitSet.set(50);bitSet.set(6);bitSet.set(10);int fromIndex = 12, toIndex = 10;bitSet.set(fromIndex, toIndex, false);System.out.println(bitSet);}}
Output:
Exception in thread "main" java.lang.IndexOutOfBoundsException: fromIndex: 12 > toIndex: 10 at java.base/java.util.BitSet.checkRange(BitSet.java:369) at java.base/java.util.BitSet.clear(BitSet.java:567) at java.base/java.util.BitSet.set(BitSet.java:531)
No comments:
Post a Comment