Kotlin

[코틀린] forEach, forEachIndexed

Sky Titan 2020. 9. 24. 21:28
728x90
 

forEach - Kotlin Programming Language

 

kotlinlang.org

forEach

  • collections의 각 element들에 대해서 특정한 작업을 수행할 수 있도록 해준다.
  • 예시) 각 element들을 출력
    var list = arrayOf("a", "b", "c", "d")

    list.forEach { println(it) }
    
    /*결과
    a
    b
    c
    d
     */

 

forEachIndexed

  • forEach와 동일한 기능을 수행하며 value뿐 아니라 해당 value의 index까지 같이 사용할 수 있다.
    var list = arrayOf("a", "b", "c", "d")

    list.forEachIndexed { index, s ->  println("$index $s") }

    /*결과
    0 a
    1 b
    2 c
    3 d
     */
728x90