Swift Set formIntersection()

formIntersection() 方法会移除集合中不包含在给定序列中的元素。

示例

// create a set A
var A: Set = [2, 3, 5]

// create an array B
 var B = [2, 4, 6]

// form intersection between set A and array B A.formIntersection(B)
print(A) // Output: [2]

formIntersection() 语法

集合formIntersection() 方法的语法是:

set.formIntersection(otherSequence)

其中,setSet 类的一个对象。


formIntersection() 参数

formIntersection() 方法接受一个参数:

  • otherSequence - 元素的序列(主要是数组和集合)。

注意other 必须是一个有限集合。


formIntersection() 返回值

  • formIntersection() 方法不返回值。

示例:Swift Set formIntersection()

// create a set A
var A: Set = ["a", "c", "d"]

// create another set B
var B: Set = ["c", "b", "e" ]

// create an array C
var C = ["b", "c", "d"]

// form intersection between set A and set B A.formIntersection(B)
print("New A:", A)
// form intersection between array C and set B B.formIntersection(C)
print("New B:", B)

输出

New A: ["c"]
New B: ["c", "b"]

在这里,我们使用了 formIntersection() 方法来移除集合 A 和 B 中不共有的元素,并最终打印更新后的集合 A 和 B。

你觉得这篇文章有帮助吗?

我们的高级学习平台,凭借十多年的经验和数千条反馈创建。

以前所未有的方式学习和提高您的编程技能。

试用 Programiz PRO
  • 交互式课程
  • 证书
  • AI 帮助
  • 2000+ 挑战