
kotlin.collections.Arrays.kt Maven / Gradle / Ivy
@file:kotlin.jvm.JvmMultifileClass
@file:kotlin.jvm.JvmName("ArraysKt")
package kotlin
import java.util.*
/**
* Returns an array with the specified [size], where each element is calculated by calling the specified
* [init] function. The `init` function returns an array element given its index.
*/
public inline fun Array(size: Int, init: (Int) -> T): Array {
val result = arrayOfNulls(size)
for (i in 0..size - 1) {
result[i] = init(i)
}
return result as Array
}
/**
* Returns an empty array of the specified type [T].
*/
public inline fun emptyArray(): Array = arrayOfNulls(0) as Array
/**
* Returns a single list of all elements from all arrays in the given array.
*/
public fun Array>.flatten(): List {
val result = ArrayList(sumBy { it.size() })
for (element in this) {
result.addAll(element)
}
return result
}
/**
* Returns a pair of lists, where
* *first* list is built from the first values of each pair from this array,
* *second* list is built from the second values of each pair from this array.
*/
public fun Array>.unzip(): Pair, List> {
val listT = ArrayList(size())
val listR = ArrayList(size())
for (pair in this) {
listT.add(pair.first)
listR.add(pair.second)
}
return listT to listR
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy