输出
输出
查询 colors.contains("1")输出
删除
删除指定某个元素
colors.remove("2")输出
删除第一个 colors.removeFirst() 删除所有 colors.removeAll()输出
//有序遍历 for color in colors1.sorted() { print(color) }输出
输出
根据两个Set 不同的值创建一个新的Set let animals4 : Set = animals1.symmetricDifference(animals2)输出
根据两个Set 中的所有值创建一个新的Set let animals5 : Set = animals1.union(animals2)输出
根据 animals1 包含但是 animals2 不包含的值创建一个新的Set let animals6 : Set = animals1.subtracting(animals2)输出
Swift 中提供了操作符和方法来判断 Set 之间的关系 是否相等 运算符 “==” 判断两个Set 的值是否全部相等
首先创建 Set let a : Set = [1,2,3,4,5] let b : Set = [1,2] let c : Set = [4,5,6,7,8] let d : Set = [1,2] 判断b 中的值是否都被 a包含 print(b.isSubset(of: a)) 判断a 是否包含 b 中的所有值 print(a.isSuperset(of: b)) 判断 c 是不是 d 的子集 并且两个 Set 不相等 print(c.isStrictSubset(of: d)) 判断 c 是不是 d 的父集 并且两个 Set 不相等 print(c.isStrictSuperset(of: d)) 判断 c 和 d 没有有交集 (不含有相同的值) print(c.isDisjoint(with: d))