004_swift_set 集合

//
//  main.swift
//  004_集合
//
//  Created by liuan on 2020/3/22.
//  Copyright © 2020 liuan. All rights reserved.
//

import Foundation
//集合
//特点。无序 不重复
var set1:Set<Int>=[1,2,3,4,5]
var set2=[1,2,3,3,5]
print(set1,set2);

//操作
//集合的总数
let count=set1.count;
print(count)
//判断是否为空
let ret1=set1.isEmpty
//是否包含某个元素
let ret2=set1.contains(3)
//遍历
for ele in set1{
    print(ele)
}
//集合最值
let min=set1.min();
let max=set1.max();
//插入元素
set1.insert(5);
//删除元素
set1.remove(1);
set1.removeFirst();
//set1.removeAll();
//数学运算(交集 intersection 补集 union 并集 subtracting)
let set3=set1.intersection(set2);
print(set3)
let set4=set1.union(set2)
print(set4)
let set5=set1.subtracting(set2);
print(set5)


效果展示

[2, 5, 1, 3, 4] [1, 2, 3, 3, 5]
5
2
5
1
3
4
[5, 3]
[2, 5, 1, 3, 4]
[4]
发布了2022 篇原创文章 · 获赞 536 · 访问量 273万+

猜你喜欢

转载自blog.csdn.net/mp624183768/article/details/105032907
今日推荐