Home » Java programming language

Java BitSet length() Method with Example

BitSet Class length() method: Here, we are going to learn about the length() method of BitSet Class with its syntax and example.
Submitted by Preeti Jain, on January 22, 2020

BitSet Class length() method

  • length() method is available in java.util package.
  • length() method is used to return the length of this BitSet otherwise it returns 0 when this BitSet is "empty".
        Length= (The indices of largest set bit+1)
    
  • length() method is a non-static method, so it is accessible with the class object and if we try to access the method with the class name then we will get an error.
  • length() method does not throw an exception at the time of returning the length of this BitSet.

Syntax:

    public int length();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of this method is int, it returns the size of this BitSet in integer.

Example:

// Java program to demonstrate the example 
// of int length() method of BitSet.

import java.util.*;

public class LengthOfBitSet {
    public static void main(String[] args) {
        // create an object of BitSet
        BitSet bs = new BitSet(10);

        // By using set() method is to set
        // the values in BitSet 
        bs.set(10);
        bs.set(20);
        bs.set(30);
        bs.set(40);
        bs.set(50);

        // Display Bitset
        System.out.println("bs: " + bs);

        // By using length() method is to return
        // the length of this BitSet
        int len = bs.length();

        // Display length of bs
        System.out.println("bs.length(): " + len);
    }
}

Output

bs: {10, 20, 30, 40, 50}
bs.length(): 51


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.