美文网首页Swiftswift
swift 中数组遍历的7种写法

swift 中数组遍历的7种写法

作者: coderLYH | 来源:发表于2018-01-06 23:27 被阅读0次

swift 中数组遍历的7种写法

列举如下, <最后一种是错误的写法>

let array = ["元素1","元素2","元素3"]

1. 区间遍历

    print("方法1************")
    for i in 0..<array.count{
        print(array[i])
    }

2. for in 遍历

    print("方法2************")
    for s in array{
        print(s)
    }

3.元组遍历

    print("方法3************")
    for s in array.enumerated(){
        print(s.offset, s.element)   
    }

4.元组

    print("方法4************")
    for (index, element) in array.enumerated(){
        print(index,element)
    }

5. 元组遍历

    print("方法5************")
    for s  in array.enumerated(){
        print(s.0,s.1)
    }

6. 反序遍历

    print("方法6 反序************")
    for s in array.enumerated().reversed(){  
        print(s.0,s.1)
    }  

7. 反序遍历

    print("方法7 反序************")
    for s in array.reversed(){ 
        print(s)
    }

8. ****错误写法 , 序号和元素没有一一对应****

  ****  /***************  错误写法**************/    ****
    print("****反序  错误写法********")
    for s in array.reversed().enumerated(){  
        print(s.0,s.1)
    }

// 打印结果如下
方法1************
元素1
元素2
元素3
方法2************
元素1
元素2
元素3
方法3************
0 元素1
1 元素2
2 元素3
方法4************
0 元素1
1 元素2
2 元素3
方法5************
0 元素1
1 元素2
2 元素3
方法6 反序************
2 元素3
1 元素2
0 元素1
方法7 反序************
元素3
元素2
元素1
****反序 错误写法********
0 元素3
1 元素2
2 元素1

相关文章

  • Swift 基础笔记 - 数组

    OC中定义数组 Swift中定义数组 初始化空数组 定义数组时指定数组类型 遍历数组中的所有元素(传统写法) 不建...

  • swift 中数组遍历的7种写法

    swift 中数组遍历的7种写法 列举如下, <最后一种是错误的写法> let array = ["元素1","元...

  • Swift 基础语法学习(五)

    Swift 中数组元素的遍历

  • Swift之数组

    swift中数组的一些用法。 数组的定义 数组的遍历 数组的增加 总结 主要说了数组的定义以及数组的遍历的方法。

  • Swift5 数组(Array)操作

    Swift数组创建 Swift获取指定位置数据 Swift数组遍历 for、map、flatMap、reduce ...

  • tips 持续更新

    在swift中的变量一般分为三种: swift - 类在数组中的写法 swift - 状态栏隐藏方法 xcode ...

  • Swift基础-03(数组和字典)

    1.Swift中数组基本使用 数组的基本使用 `` 数组的遍历 数组的增删改 数组容量,这个在OC中我们经常使用在...

  • 2019-05-05: 五:Swift中数组的使用?

    一:Swift中数组的使用? 二:数组的介绍? 三:数组的初始化? 四:对数组的基本操作? 五:数组的遍历? 六;...

  • Swift之for-in循环

    Swift中没有了for-of循环,大部分遍历内容都落在了for-in的身上。 一、基本用法 遍历数组内容 遍历字...

  • c基础—指针运算和函数指针

    二级指针 数组和数组指针 采用指针遍历数组循环赋值 遍历 赋值 指针与数组的几种写法 函数指针(回调) 题目:监听...

网友评论

    本文标题:swift 中数组遍历的7种写法

    本文链接:https://www.haomeiwen.com/subject/xgvunxtx.html