티스토리 뷰
Computer Language/Kotlin
[Kotlin] How to convert List to an array in Kotlin?
HarryNam 2019. 9. 3. 08:28First, implement convert function
private inline fun <reified T> toArray(list: List<*>): Array<T> {
return (list as List<T>).toTypedArray()
}and then convert list to array with this function
val list:List<String> = lisfOf("harry", "potter")
val array = toArray<String>(list)If you need to put array value to vararg parameter to Java Function, you need to put * front of variable
val familyName = getFamilyName(*array)When will vararg needed? : when you need to import java library and a function which using vararg like below
String familyName = getFamilyName(String... name);'Computer Language > Kotlin' 카테고리의 다른 글
| [TroubleShooting] CoroutineContext version up issue (0) | 2019.08.30 |
|---|---|
| [TroubleShooting] Kotlin not configured in IntelliJ (0) | 2019.08.27 |
| [Kotlin] How to use Null safely (0) | 2019.08.26 |
| [Kotlin] Null 을 안전하게 사용하는 방법 (0) | 2019.08.07 |
댓글
TAG
more
Recent Post