当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java BitSet intersects()用法及代码示例


BitSet是java.util包中定义的类。它创建一个由0和1表示的位数组。

句法

public boolean intersects(BitSet set)

参数:该方法接受强制性参数集,该参数集是要检查交集的BitSet。


返回:该方法返回一个布尔值,指示此BitSet是否与指定的BitSet相交

说明:如果指定的BitSet具有设置为true的任何位,并且在此BitSet中也设置为true,则该方法返回true,否则返回false。

例子:

Input:
set1:{1, 2, 4}
set2:{}

Output:
false
// 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(); 
  
        /* set is BitSet class method 
        expalined in next articles */
        bs1.set(0); 
        bs1.set(1); 
        bs1.set(2); 
        bs1.set(4); 
  
        // assign values to bs2 
        bs2.set(4); 
        bs2.set(6); 
        bs2.set(5); 
        bs2.set(1); 
        bs2.set(2); 
        bs2.set(3); 
  
        // Printing the 2 Bitsets 
        System.out.println("bs1:" + bs1); 
        System.out.println("bs2:" + bs2); 
        System.out.println("bs3:" + bs3); 
  
        // Performing intersects on two bitset 
        System.out.println("bs1 intersection bs3 = "
                           + bs1.intersects(bs2)); 
        System.out.println("bs3 intersection bs1 = "
                           + bs3.intersects(bs1)); 
    } 
}
输出:
bs1:{0, 1, 2, 4}
bs2:{1, 2, 3, 4, 5, 6}
bs3:{}
bs1 intersection bs3 = true
bs3 intersection bs1 = false

相关文章:

  1. 位集类示例集1
  2. 位集类示例集2
  3. 位集类示例集3


相关用法


注:本文由纯净天空筛选整理自barykrg大神的英文原创作品 Java BitSet | intersects()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。