← 返回列表

Kotlin 循环控制 for 语句

Kotlin for 语句可以对任何提供迭代器(iterator)的对象进行遍历 for (item in collection) print(item) 循环体可以是一个代码块: for (item: Int in ints) { // …… } 如上所述,for 可以循环遍历任何提供了迭代器的对象。 如果你想要通过索引遍历一个数组或者一个 list,你可以这么做: for (i in array.indices) { print(array[i]) } 这种"在区间上遍历"会编译成优化的实现而不会创建额外对象 或者你可以用库函数 withIndex for ((index, value) in array.withIndex()) { println("the element at $index is $value") } 对集合进行迭代: // filename: main.kt // author: 简单教程(www.twle.cn) // Copyright © 2015-2065 www.twle.cn. All rights reserved. fun main(args: Array<String>) { val items = listOf("百度", "腾讯", "阿里巴巴") for (item in items) { println(item) } for (index in items.indices) { println("item at $index is ${items[index]}") } } 编译运行以上 Kotlin 范例,输出结果如下 $ kotlinc main.kt -include-runtime -d main.jar $ java -jar main.jar 百度 腾讯 阿里巴巴 item at 0 is 百度 item at 1 is 腾讯 item at 2 is 阿里巴巴

Kotlin for 语句可以对任何提供迭代器(iterator)的对象进行遍历

for (item in collection) print(item)

循环体可以是一个代码块:

for (item: Int in ints) {
    // ……
}

如上所述,for 可以循环遍历任何提供了迭代器的对象。

如果你想要通过索引遍历一个数组或者一个 list,你可以这么做:

for (i in array.indices) {
    print(array[i])
}

这种"在区间上遍历"会编译成优化的实现而不会创建额外对象

或者你可以用库函数 withIndex

for ((index, value) in array.withIndex()) {
    println("the element at $index is $value")
}

对集合进行迭代:

// filename: main.kt
// author: 简单教程(www.twle.cn)
// Copyright © 2015-2065 www.twle.cn. All rights reserved.

fun main(args: Array<String>) {
    val items = listOf("百度", "腾讯", "阿里巴巴")
    for (item in items) {
        println(item)
    }

    for (index in items.indices) {
        println("item at $index is ${items[index]}")
    }
}

编译运行以上 Kotlin 范例,输出结果如下

$ kotlinc main.kt -include-runtime -d main.jar 
$ java -jar main.jar
百度
腾讯
阿里巴巴
item at 0 is 百度
item at 1 is 腾讯
item at 2 is 阿里巴巴

打开资源 ↗

交流与评论