How to convert List to an array in Kotlin?

less than 1 minute read

First, 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);