각 요소에 대해 지정된 작업을 수행합니다.
public inline fun <T> Iterator<T>.forEach(operation: (T) -> Unit): Unit {
for (element in this) operation(element)
}
val list = listOf(1, 2, 3, 4)
list.forEach { num -> print(num) } // 1234
각 요소에 대해 지정된 작업을 수행하여 요소에 순차적 인덱스를 제공합니다.
public inline fun <T> Iterable<T>.forEachIndexed(action: (index: Int, T) -> Unit): Unit {
var index = 0
for (item in this) action(checkIndexOverflow(index++), item)
}
"Hello".forEachIndexed { i, v: Char -> print("$i$v ") } // 0H 1e 2l 3l 4o
'코틀린 > [Elements] 요소 작업' 카테고리의 다른 글
[Kotlin][Collection] first / firstOrNull (0) | 2024.08.20 |
---|---|
[Kotlin][Collection] elementAt / elementAtOrElse / elementAtOrNull (0) | 2024.08.19 |
[Kotlin][Collection] contains (0) | 2024.08.19 |