isDisjoint()
方法如果两个集合是互斥集,则返回 true。如果不是,则返回 false。
示例
var A: Set = [1, 2, 3, 4, 5]
var B: Set = [12,13,14]
// check if A is disjoint with B or not
print(A.isDisjoint(with: B))
// Output: true
isDisjoint() 语法
集合 isDisjoint()
方法的语法是
set.isDisjoint(otherSet)
其中,set 是 Set
类的一个对象。
isDisjoint() 参数
isDisjoint()
方法接受一个参数
- otherSet - 元素的集合。
isDisjoint() 返回值
- 如果 set 与 otherSet 互斥,则
isDisjoint()
方法返回true
。如果不是,则返回false
。
示例:Swift Set isDisjoint()
var A: Set = [1, 2, 3, 4]
var B: Set = [5, 6, 7]
var C: Set = [4, 5, 6]
// check if A and B are disjoint or not
print("Are A and B disjoint?", A.isDisjoint(with: B))
// check if A and C are disjoint or not
print("Are A and C disjoint?", A.isDisjoint(with: C))
输出
Are A and B disjoint? true Are A and C disjoint? false
在这里,我们使用 isDisjoint()
方法来检查两个集合是否互斥。
由于
- A 和 B 拥有唯一的元素,该方法返回
true
。 - A 和 C 都拥有元素 4,该方法返回
false
。