BitSet是java.util包中定义的类。它创建一个由0和1表示的位数组。
句法 public int cardinality()
cardinality()方法用于返回set()此Bitset中的基数(即,使用method设置为true的位数)。
例子:
Input:
set1:{1, 2, 4}
set2:{}
Output:
3
0
// Java program illustrating Bitset Class functions.
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Constructors of BitSet class
BitSet bs1 = new BitSet();
BitSet bs2 = new BitSet();
BitSet bs3 = new BitSet();
bs1.set(0);
bs1.set(1);
bs1.set(2);
bs1.set(4);
bs2.set(4);
bs2.set(6);
bs2.set(5);
bs2.set(1);
bs2.set(2);
bs2.set(3);
System.out.println("bs1:" + bs1);
System.out.println("bs2:" + bs2);
System.out.println("bs3:" + bs3);
// Performing logical AND
// on bs2 set with bs1
System.out.println(bs1.cardinality());
System.out.println(bs2.cardinality());
System.out.println(bs3.cardinality());
}
}
输出:
bs1:{0, 1, 2, 4}
bs2:{1, 2, 3, 4, 5, 6}
bs3:{}
4
6
0
java bitset
于 2022-08-31 11:47:19 首次发布