Swift 学习之集合的基本用法
来源:互联网 发布:python urlencode 编辑:程序博客网 时间:2024/06/05 19:39
集合的基本用法
数组:有序的数据
1.定义一个数组,并初始化的不同方式
var array1 = []var array2 :[String] = ["hello"]var array3 = [String](count: 1, repeatedValue: "world")var array4 = array2 + array3 // "hello world"if !array4.isEmpty{ print(array4) //hello world}
2.向数组中添加元素或者是修改某个元素的值
array4.append("!")array4 += ["..."]array4 += ["...","....."]array4.insert("first code ", atIndex: 0)array4[0] = "first Item"
3.删除元素的时候,一般都回返回删除的元素
array4.removeAtIndex(0) //删除元素并返回该删除的元素array4.removeFirst() //"hello"array4.removeLast() "....."//array4.removeAll() //删除所有元素
4.访问数组元素的值
array4[0] for item in array4{ print(item)}for (index,value) in array4.enumerate(){ print("\(index + 1):\(value)")}
Set :无序的数据集合
1.初始化
var letters = Set<String>()print("letters is of type Set<Character> with \(letters.count) items.")var letters1 :Set = ["1","2","3","4","6","7"]var letters2 :Set = ["2","3"]var letters3 :Set = ["3","8","5"]
2.访问还有修改set的元素
letters1.insert("8")letters1.countif !letters1.isEmpty{ print(letters1)}if let tempItem = letters1.remove("3"){ print(tempItem)}if letters1.contains("1"){ print("letters1 contains 1")}for item in letters1{ print(item)}for item in letters1.sort(){ print(item)}//letters1 = []letters1.removeAll()
3.set 之间的基本操作
var data1 :Set = ["1","2","3","4","6","7"]var data2 :Set = ["2","3"]var data3 :Set = ["4","8","5"]// intersect : 返回set之间相同的数据 data1.intersect(data2) //{"2", "3"}// exclusiveOr:返回两者之间不同的部分 data1.exclusiveOr(data3) //{"2", "1", "8", "6", "4", "7", "5"}//union :返回两者之间所有的数据data1.union(data3) //{"2", "1", "8", "6", "4", "3", "7", "5"}//subTractdata1.subtract(data2) //返回data1中data2不含有的元素//比较两个setif data1 != data2{ print("\(data1) != \(data2)") //"["4", "2", "1", "7", "6", "3"] != ["2", "3"]}// 是否是父set if data1.isSupersetOf(data2){ print("\(data1) 包含 \(data2)") // "["4", "2", "1", "7", "6", "3"] 包含 ["2", "3"]\n" }//是否是子set if data2.isSubsetOf(data1){ print("\(data2) 是 \(data1) 的子set") //"["2", "3"] 是 ["4", "2", "1", "7", "6", "3"] 的子set\n" }// 是否是子set,但是不相同 if data2.isStrictSubsetOf(data1){ print(data2) //"["2", "3"]\n" }// 两个set是否含有相同的元素 if data2.isDisjointWith(data3){ print(data2) //"["2", "3"]\n" }
dictonry 字典。无序的键值对
1.初始化
var dict1 :Dictionary = [Int : String ]()dict1 = [1:"one",2:"two",3:"three",4:"four"]var dict2:[String:String] = ["1":"one","2":"two","3":"three","4":"four"]
2.访问或者修改字典的元素
dict2.count //4if !dict2.isEmpty{ print(dict2) // "["4": "four", "2": "two", "1": "one", "3": "three"]\n"}dict2["3"] = "三"print(dict2) //"["4": "four", "2": "two", "1": "one", "3": "三"]\n"//更改元素并返回if let item = dict2.updateValue("three", forKey: "3"){ print(item) //"三\n"}if let item = dict2["1"]{ print(item) //one}dict2.removeValueForKey("4") //four,删除指定key的元素,并返回//遍历for (key ,value) in dict2 { print("\(key) : \(value)")}for key in dict2.values{ print(key)}for value in dict2.values{ print(value)}//转换为数组let demos = [String](dict2.values)print(demos)
0 0
- Swift 学习之集合的基本用法
- Swift学习之UILabel基本用法
- Swift学习之--TableView的基本使用
- Swift 基本知识点之二集合介绍
- Swift学习之基本语法
- Swift学习之集合的reduce 操作详解
- Swift开发笔记之UIButton基本用法
- JAVA学习【8】集合框架HashMap、TreeMap的基本用法
- 集合Collection的基本用法
- IOS Swift学习之集合类型
- swift-元组的基本用法
- Swift 变量名、print的基本用法
- swift 字符串String的基本用法
- swift 协议Protocol的基本用法
- 二 Swift学习之基本运算符
- [swift学习之二]基本语法练习
- unity学习之列的基本操作和集合
- swift基本用法
- 将GridView中的数据导出到Excel / Word中
- 【JAVA笔记——道】JAVA内存操作 sun.misc.Unsafe类
- C语言变量
- Handler工作机制【简】——学习笔记
- cgi stress test, mongoose dead
- Swift 学习之集合的基本用法
- 使用注解的Hibernate one-to-one映射
- Hibernate基本特性二 -- 一级缓存
- 并查集
- NFC手机识别身份证的技术实现思路
- aabbaa
- Java+MySQL实现网络爬虫程序
- 随想录(关于smp的均衡负载)
- hdoj 2476 String painter 【区间dp】