Development/Kotlin

[kotlin] for문 에서 collection withIndex 사용하기

Sonagiya 2021. 4. 2. 14:16
728x90
반응형

자바에서는 collection을 사용하여 for문을 돌릴때 i = 0, ... 

kotlin collection 에는 vlaue와 index 를 모두 받는 withIndex 가 있다.

    val iterator = ('a'..'c').iterator()

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

 

(kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/with-index.html)

 

 

 

 

 

반응형