본문 바로가기
코틀린/[Mapping] 매핑 작업

[Kotlin][Collection] toTypedArray

by jinwo_o 2024. 8. 30.

이 컬렉션의 모든 요소를 ​​포함하는 형식화된 배열을 반환합니다.

이 컬렉션의 크기와 동일한 크기를 갖는 런타임 유형 T 의 배열을 할당하고 이 컬렉션의 요소로 배열을 채웁니다.

public actual inline fun <reified T> Collection<T>.toTypedArray(): Array<T> {
    val thisCollection = this as java.util.Collection<T>
    return thisCollection.toArray(arrayOfNulls<T>(0)) as Array<T>
}


val collection = listOf(1, 2, 3)
val array = collection.toTypedArray()
println(array.contentToString()) // [1, 2, 3]

 

toTypedArray - Kotlin Programming Language

 

kotlinlang.org

'코틀린 > [Mapping] 매핑 작업' 카테고리의 다른 글

[Kotlin][String] trim / trimStart / trimEnd  (0) 2024.08.30
[Kotlin][String] replaceRange  (0) 2024.08.27
[Kotlin][String] replace  (0) 2024.08.27