본문 바로가기
Development/Kotlin

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

by Sonagiya 2021. 4. 2.
반응형

자바에서는 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)

 

 

 

 

 

반응형

'Development > Kotlin' 카테고리의 다른 글

[kotlin] also, let, apply, run, with  (0) 2021.04.08
Kotlin cheat sheet  (0) 2021.01.05
코틀린 문서 벅역본  (0) 2017.03.09

댓글