kotlin学习 - Kotlin 循环
1 2 3 4 5 6 7
|
for (i in 1..10) { println(i) }
|
1 2 3 4 5 6 7
|
for (i in 1 until 10) { println(i) }
|
1 2 3 4 5 6
| for (i in 10 downTo 1) { println(i) }
|
1 2 3 4 5 6
| for (i in 1..10 step 2) { println(i) }
|
repeat(count:Int) 一个高阶的循环函数,count代表循环的次数,在函数内,it代表到第几次
注意:it是从0开始计数的
1 2 3 4 5 6
| repeat(10) { println(it) }
|
1 2 3 4 5 6 7
| val list = arrayListOf<String>("A", "B", "C", "D") for (str in list) { println(str) }
|
1 2 3 4 5 6 7
| val list = arrayListOf<String>("A", "B", "C", "D") for ((index,str) in list.withIndex()){ println("第${index}个的元素是$str") }
|