[Swift]遍历集合类型(数组、集合和字典)
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/ )
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9798475.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
Swift提供了三种主要的集合类型,称为数组,集合和字典,用于存储值集合。
数组是有序的值集合。
集是唯一值的无序集合。
字典是键值关联的无序集合。
Swift中无法再使用传统形式的for循环。
1 //传统for循环形式不适用于Swift 2 for(单次表达式;条件表达式;末尾循环体){中间循环体;}
遍历数组:可以使用for
- in
循环遍历数组中的值。
1 let arrayStr:[String] = ["s","t","r","e","n","g","t","h","e","n","!"] 2 3 //方法1:根据元素顺序遍历 4 for item in arrayStr { 5 print(item) 6 } 7 8 //方法2:根据部分元素顺序遍历 9 for item in arrayStr[1...5] { 10 print(item) 11 } 12 13 //方法3:根据索引顺序遍历 14 for index in 0...(arrayStr.count - 1) { 15 print(arrayStr[index]) 16 } 17 18 //方法4:初始化数组并顺序遍历 19 for item in Array(1...10) { 20 print(item) 21 } 22 23 //方法5:stride函数 24 //from,to,by 最后一个值是 >(或<) to的值 25 //from,through,by 最后一个值是 >=(或<=) through的值 26 for index in stride(from: 0, to: arrayStr.count,by: 1 ) 27 { 28 print(arrayStr[index]) 29 }
如果不需要序列中的每个值,则可以使用下划线(_)代替变量名来忽略这些值。
1 let base = 3 2 let power = 10 3 var answer = 1 4 for _ in 1...power { 5 answer *= base 6 } 7 print("\(base) to the power of \(power) is \(answer)") 8 // Prints "3 to the power of 10 is 59049"
遍历集合:可以使用for
- in
循环遍历集合中的值。
1 let arrayStr:Set<String> = ["s","t","r","e","n","g"] 2 //输出的顺序是随机的 3 for item in arrayStr { 4 print("\(item)") 5 }
Swift的Set
类型没有定义的顺序。要以特定顺序迭代集合的值,请使用该sorted()
方法,该方法将集合的元素作为使用<
运算符排序的数组返回。
1 let arrayStr:Set<String> = ["s","t","r","e","n","g"] 2 //按照字母排序顺序输出 3 for item in arrayStr.sorted() { 4 print("\(item)") 5 }
遍历字典(可单独遍历键、值、或键值)
可以用字典遍历键值对for
- in
环。字典中的每个项都作为元组返回,您可以将元组的成员分解为临时常量或变量,作为迭代的一部分:(key, value),
还可以通过访问字典的键keys
和values
属性来检索字典的键或值的可迭代集合:
1 var direc: [Int: String] = [0:"s",1:"t",2:"r",3:"e",4:"n",5:"g"] 2 3 //按键值对遍历顺序遍历 4 for (num, char) in direc.sorted(by: <) { 5 print("\(num): \(char)") 6 } 7 8 //按键遍历顺序遍历 9 for num in direc.keys.sorted(by: <) { 10 print("\(num)") 11 } 12 13 //按值遍历顺序遍历 14 for char in direc.values { 15 print("\(char)") 16 }
如果您需要使用带有Array
实例的API的字典键或值,请使用keys
或values
属性初始化新数组:
1 let nums = [Int](direc.keys) 2 print(nums) 3 //次序随机 [4, 5, 2, 0, 1, 3] 4 5 let chars = [String](direc.values) 6 print(chars) 7 //次序随机 ["n", "g", "r", "s", "t", "e"]
Swift的Dictionary
类型没有定义的顺序。要按特定顺序迭代字典的键或值,请sorted()
在其keys
或values
属性上使用该方法。
倒序遍历 :reversed() 或 stride函数
Swift中标准库中方法
1 //可以用来快速的枚举数组中的内容,并用元组(index, value)接收 2 public func enumerated() -> EnumeratedSequence<Array<Element>> 3 4 //可以倒叙数组内容 5 public func reversed() -> ReversedRandomAccessCollection<Array<Element>>
当两者结合使用用到索引时需要注意
1 let arrayStr:[String] = ["s","t","r","e","n","g","t","h","e","n","!"] 2 3 //索引元素值绑定一起倒序遍历 4 for i in arrayStr.enumerated().reversed() { 5 print("\(i) ", terminator: "") 6 } 7 /* 8 (offset: 10, element: "!") (offset: 9, element: "n") (offset: 8, element: "e") (offset: 7, element: "h") (offset: 6, element: "t") (offset: 5, element: "g") (offset: 4, element: "n") (offset: 3, element: "e") (offset: 2, element: "r") (offset: 1, element: "t") (offset: 0, element: "s") 9 */ 10 11 //只元素值倒序遍历,原索引不变 12 for i in arrayStr.reversed().enumerated() { 13 print("\(i) ", terminator: "") 14 } 15 /* 16 (offset: 0, element: "!") (offset: 1, element: "n") (offset: 2, element: "e") (offset: 3, element: "h") (offset: 4, element: "t") (offset: 5, element: "g") (offset: 6, element: "n") (offset: 7, element: "e") (offset: 8, element: "r") (offset: 9, element: "t") (offset: 10, element: "s") 17 */
倒序遍历数组
1 let arrayStr:[String] = ["s","t","r","e","n","g","t","h","e","n","!"] 2 3 //方法1:根据元素倒序遍历 4 for item in arrayStr.reversed() { 5 print(item) 6 } 7 8 //方法2:根据部分元素倒序遍历 9 for item in arrayStr[1...5].reversed() { 10 print(item) 11 } 12 13 //方法3:根据索引倒序遍历 14 for index in (0...(arrayStr.count - 1)).reversed() { 15 print(arrayStr[index]) 16 } 17 18 //方法4:初始化数组并倒序遍历 19 for item in Array(1...10).reversed() { 20 print(item) 21 } 22 23 //方法4:初始化数组并倒序遍历 24 for item in Array(1...10).reversed() { 25 print(item) 26 } 27 28 //方法5:stride函数 29 //from,to,by 最后一个值是 >(或<) to的值 30 //from,through,by 最后一个值是 >=(或<=) through的值 31 for index in stride(from: arrayStr.count - 1, through: 0,by: -1 ) 32 { 33 print(arrayStr[index]) 34 }
倒序遍历集合
1 let arrayStr:Set<String> = ["s","t","r","e","n","g"] 2 //按照字母排序倒序输出 3 for item in arrayStr.sorted().reversed() { 4 print("\(item) ", terminator: "") 5 } 6 //Print "t s r n g e"
倒序遍历字典(可单独遍历键、值、或键值)
1 var direc: [Int: String] = [0:"s",1:"t",2:"r",3:"e",4:"n",5:"g"] 2 //按键值对倒序遍历 3 for (num, char) in direc.sorted(by: <).reversed() { 4 print("\(num): \(char)") 5 }