ThinkSSL🔒 一键申购 5分钟快速签发 30天无理由退款 购买更放心 广告
### for 循环 [TOC] for循环语句是最常用的循环语句,一般用在循环次数已知的情况下。for循环语句的语法格式如下: ``` for (循环对象 in 循环条件) { 执行语句 … } ``` 在上面的语法结构中,for关键字后边()中包括了3部分内容:循环对象、in和循环条件,{}中的执行语句为循环体,in表示在某个范围内。 示例 ``` fun main(args: Array<String>) { // 循环4次,且步长为1的递增,0..3表示[0,3] 之间的数字 for (i in 0..3) { //i的值会在0~3之间变化 println("i => $i \t") } } ``` 运行结果 ``` i => 0 i => 1 i => 2 i => 3 ``` 在上述代码中可以看到,循环条件是“0..3”,这个循环条件的意思是0<=i<=3,当变量i的值在[0,3]这个范围内时,会执行循环体输出i的值,变量i的值是从0开始的,每次执行完循环体后变量i的值会自增1(即步长默认为1),直到变量i=4时,不符合条件“0..3”,结束循环。 for 循环可以对任何提供迭代器(iterator)的对象进行遍历,相当于像 C# 这样的语言中的`foreach`循环,语法如下: ``` for (item in collection) print(item) for (item in collection) { print(item) } ``` 在使用for循环语句的过程中,如果for循环中只有一条执行语句,则可以去掉循环体的{},简写为如下形式 ``` fun main(args: Array<String>) { //循环语句只有一条时,可以进行简写 for (i in 0..3) println("i => $i \t") } ``` 循环体可以是一个代码块 ``` for (item: Int in ints) { // …… } ``` 如上所述,for可以循环遍历任何提供了迭代器的对象。即: * 有一个成员函数或者扩展函数`iterator()`,它的返回类型 * 有一个成员函数或者扩展函数`next()`,并且 * 有一个成员函数或者扩展函数`hasNext()`返回`Boolean`。 这三个函数都需要标记为`operator`。 如需在数字区间上迭代,请使用[区间表达式](http://www.kotlincn.net/docs/reference/ranges.html): ``` for (i in 1..3) { println(i) } for (i in 6 downTo 0 step 2) { println(i) } ``` 运行结果 ``` 1 2 3 6 4 2 0 ``` >[info]注意:对区间或者数组的`for`循环会被编译为并不创建迭代器的基于索引的循环 如果你想要通过索引遍历一个数组或者一个 list,你可以这么做: ``` for (i in array.indices) { println(array[i]) } ``` 或者你可以用库函数 withIndex : ``` for ((index, value) in array.withIndex()) { println("the element at $index is $value") } ``` 示例 ~~~ fun main(args: Array<String>) { val items = arrayOf(1, 2, 3, 4, 5, "a") println("--------最基本的遍历--------") for (item in items) { println(item) } println("--------遍历的时候,得到索引--------") for (index in items.indices) { println("$index->${items[index]}") } println("--------遍历的时候,得到索引和值--------") for ((index, value) in items.withIndex()) { println("$index->$value") } } ~~~ 运行结果 ``` --------最基本的遍历-------- 1 2 3 4 5 a --------遍历的时候,得到索引-------- 0->1 1->2 2->3 3->4 4->5 5->a --------遍历的时候,得到索引和值-------- 0->1 1->2 2->3 3->4 4->5 5->a ``` ### 循环嵌套 循环嵌套是指在一个循环语句的循环体中再定义一个循环语句的语法结构,while、do…while、for循环语句都可以进行嵌套,并且它们之间也可以相互嵌套,如最常见的for循环中嵌套for循环,格式如下: ``` for (循环对象in循环条件) { … for (循环对象in循环条件) { 执行语句 … } … } ``` 示例:使用“*”打印直角三角形 ``` fun main(args: Array<String>) { // 循环6次,且步长为1的递增,0..5表示[0,5]之间 for (i in 0..5) { //外层循环 for (j in 0..i) { //内层循环 print("*") //打印* } print("\n") //换行 } } ``` 运行结果 ``` * ** *** **** ***** ****** ``` 对应Java中的代码如下 ``` class Test { public static void main(String[] args) { for(int x = 0; x <= 5; x++){ for(int y = 0; y <= x ; y++){ System.out.print("*" ); } System.out.println(); } } } ``` ### forEach循环语句 forEach循环语句,这个循环语句在遍历数组和集合方面(后面会详细讲解,此处了解即可)为开发人员提供了极大的方便。 #### 普通的forEach语句 普通的forEach语句的格式如下: ``` 调用者.forEach() { println("it=${it}") } ``` 上述语法格式中,调用者可以是数组或集合,it表示数组中的元素。接下来我们通过一段代码来演示forEach遍历数组中的元素 示例 ``` fun main(args: Array<String>) { var arr: IntArray = intArrayOf(1, 2, 3, 4) //定义一个数组arr并初始化该数组 arr.forEach() { print(it.toString() + "\t") //it表示数组中对应的元素对象 } } ``` 运行结果 ``` 1 2 3 4 ``` #### 带角标的forEachIndexed语句 带角标的forEachIndexed语句的格式如下: ``` 调用者.forEachIndexed() { index, it -> println("角标=$index元素=${it}") } ``` 上述语法格式中,index表示数组角标或者是集合的索引,it表示数组角标或者是集合索引中对应的元素。接下来我们通过一段代码来演示forEachIndexed遍历数组中的元素以及角标 ``` fun main(args: Array<String>) { var arr: IntArray = intArrayOf(1, 2, 3, 4) //定义一个数组arr并初始化该数组 arr.forEachIndexed() { index, it -> println("角标=$index 元素=$it") } } ``` 运行结果 ``` 角标=0 元素=1 角标=1 元素=2 角标=2 元素=3 角标=3 元素=4 ```