swift 数组遍历 forEach 、enumerated、iterator、indices
//: A UIKit based Playground for presenting user interfaceimport UIKit
let fiveZs = Array(repeating: "Z", count: 5)
//数组遍历
//for in
//for each
// 无法使用break 或continue跳出或者跳过循环
// 使用retrun 只能退出当前一次的循环体let numbers=[Int](0...7)
numbers.forEach{(num) inif(num==3){return}print(num)
}
0
1
2
4
5
6
7
普通的for循环无法拿到索引
通过关键字enumerated()可以拿到索引
//: A UIKit based Playground for presenting user interfaceimport UIKit
let fiveZs = Array(repeating: "Z", count: 5)
//数组遍历
//for inlet numbers=[Int](0...7)
for (index,num) in numbers.enumerated(){print("the index is :\(index)")print(num)
}
the index is :0
0
the index is :1
1
the index is :2
2
the index is :3
3
the index is :4
4
the index is :5
5
the index is :6
6
the index is :7
7
迭代器遍历
//: A UIKit based Playground for presenting user interfaceimport UIKit
let fiveZs = Array(repeating: "Z", count: 5)
//数组遍历
//iteratorlet numbers=[Int](0...7)
var numInerator = numbers.makeIterator()
while let num = numInerator.next() {print(num)
}
0
1
2
3
4
5
6
7
讲到遍历就离不开索引
startIndex 返回第一个元素的位置,对于数组来说,永远都是0
endIndex 返回最优一个元素索引+1的位置等同于count
如果数组为空,startIndex 等于endeIndex
enumerated获取了索引和值
那么如果我们想只遍历索引呢
可以使用indices获取数组的索引区间
//: A UIKit based Playground for presenting user interfaceimport UIKit
let numbers = [Int](2...7)
for i in numbers.indices{print(numbers[i])
}
2
3
4
5
6
7
swift 数组遍历 forEach 、enumerated、iterator、indices相关推荐
- Swift数组遍历和索引
Swift数组遍历和索引 数组遍历 索引 数组遍历 Swift最常见的就是For-In 除此之外还有forEach方法,我们先来介绍一下forEach,但是forEach方法有些缺点 1.无法使用br ...
- JavaScript中的数组遍历forEach()与map()方法以及兼容写法
原理: 高级浏览器支持forEach方法 语法:forEach和map都支持2个参数:一个是回调函数(item,index,list)和上下文: forEach:用来遍历数组中的每一项:这个方法执行是 ...
- map语法获取index_JavaScript中的数组遍历forEach()与map()方法以及兼容写法
原理: 高级浏览器支持forEach方法 语法:forEach和map都支持2个参数:一个是回调函数(item,index,list)和上下文: forEach:用来遍历数组中的每一项:这个方法执行是 ...
- PHP 数组遍历 foreach 语法结构
来源:http://www.cnblogs.com/keta/p/6117237.html foreach 语法结构用于遍历数组. foreach() PHP foreach() 语法结构用于遍历操作 ...
- JS数组遍历-forEach()、map()方法
[{a:'1',b:'11'},{a:'2',b:'21'},{a:'3',b:'31'}].forEach((item, index, array) => {let c = '';let d ...
- 对数组遍历forEach、map、reduce、filter
forEach(一排人,每个人都捐点) developers.forEach(function (developer) {developer.donate(); }); map(一排人,每个人脱鞋放正 ...
- Array常用方法(数组增加、数组删除、数组截取、数组查找、数组遍历)
Array的常用方法使用 1.数组增加 push: 向数组的末尾添加新的元素(参数可以是多个),返回数组长度,改变原数组 var arr = []; var arrLength = arr.push( ...
- 二维数组的foreach遍历和三种集合的遍历
package com.skiff.www.base.grammar;import java.util.ArrayList; import java.util.Iterator; import jav ...
- 数组遍历 map()、forEach() 及 字符串切割 split() / 字符串截取 slice()、substring()、substr()...
JS数组遍历的几种方式 JS数组遍历,基本就是for,forin,foreach,forof,map等等一些方法,以下介绍几种本文分析用到的数组遍历方式以及进行性能分析对比 第一种:普通for循环 代 ...
- Java中遍历数组使用foreach循环还是for循环?
这篇文章主要介绍了Java中遍历数组使用foreach循环还是for循环?本文着重讲解for语句的语法并给出使用实例,同时总结出尽量使用foreach语句遍历数组,需要的朋友可以参考下 从JDK1.5 ...
最新文章
- python 函数调用列表,函数调用列表的Python oneliner
- got github
- Linux eval命令
- mybais传入多个参数的方法
- Linux - 将Crontab中的命令输出按照当前日期进行存储
- dataframe 如何选中某列的一行_PySpark和SparkSQL基础:如何利用Python编程执行Spark(附代码)
- 算法:POJ1007 DNA sorting
- 高大上的集团名字_最火的微信名字大全男成熟内涵高大上
- 导致溢出_邯郸一司机,溢出的“5毫克”导致A2被降级,老司机自吞“苦酒”
- html一段文字弹窗提示代码,css3提示文字弹窗代码
- 年轻时不多闯闯,老了拿什么来吹
- Graphpad prism 使用教程汇总(更新)
- 读书笔记 - 简约之美:软件设计之道
- Linux源码安装pgadmin4,CentOS7中安装pgAdmin 4
- Zabbix5 安装教程
- JavaScript学习笔记二 标识符-字符类型
- opencv: C++实现将彩色图转换为灰色图
- TTime::FormatL详解
- 人生若只如初见-云计算时代
- 0 win10重装partition_gpt分区无法安装win10的根本原因