본문 바로가기
코틀린/[Extraction] 추출 작업

[Kotlin][Collection] slice

by jinwo_o 2024. 8. 16.

지정된 인덱스 범위의 인덱스에 있는 요소를 포함하는 목록을 반환합니다.

public fun <T> List<T>.slice(indices: IntRange): List<T> {
    if (indices.isEmpty()) return listOf()
    return this.subList(indices.start, indices.endInclusive + 1).toList()
}


val numbers = listOf("one", "two", "three", "four", "five", "six")
println(numbers.slice(1..3))            // [two, three, four]
println(numbers.slice(0..4 step 2))     // [one, three, five]
println(numbers.slice(setOf(3, 5, 0)))  // [four, six, one]

 

slice - Kotlin Programming Language

 

kotlinlang.org

'코틀린 > [Extraction] 추출 작업' 카테고리의 다른 글

[Kotlin][Collection] unzip  (0) 2024.08.22
[Kotlin][Collection] take / takeLast  (0) 2024.08.16
[Kotlin][Collection] drop / dropLast  (0) 2024.08.14