20200204[轉]Kotlin-For循環

0 原文

1. For 循環

對集合進行迭代

fun main(args : Array<String>) {
    var list = listOf("No1", "No2", "No3")

    // For 循環
    for (i in list) {
        println(i)
    }

    // For 循環,並打印下標 index
    for (index in list.indices) {
        println("Index is $index, and the value is ${list[index]}")
    }
}

運行結果

No1
No2
No3
Index is 0, and the value is No1
Index is 1, and the value is No2
Index is 2, and the value is No3

Process finished with exit code 0
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章