Java BitSet intersects() 方法

2025年1月7日 | 阅读 2 分钟

Java **BitSet** 类的 intersects(BitSet set) 方法根据参数 BitSet 是否与此 BitSet 相交来返回布尔值 true 或 false。如果参数 BitSet set 在此 BitSet 中也为 true,则返回 true。

语法

参数

数据类型参数描述
BitSetset这是一个需要进行相交操作的 BitSet。

返回值

根据参数 BitSet 是否与此 BitSet 相交,它将返回布尔值 true 或 false。

Exception

NullPointerException - 如果向该方法传递了 null 参数。

兼容版本

Java 1.4 及以上版本

示例 1

输出

bitset1: {1, 2, 3, 4}
bitset2: {1, 2, 6, 7}
bitset3: {5, 6, 7, 8}
intersected result between bitset1 and bitset2: true
intersected result between bitset1 and bitset3: false

示例 2

如果传入 null 参数,此方法将抛出 NullPointerException。

输出

Exception in thread "main" java.lang.NullPointerException
	at java.util.BitSet.intersects(Unknown Source)
	at BitSetEntersectsExample2.main(BitSetEntersectsExample2.java:14)
bitset1: {1, 2, 3, 4}