Array
For Common, JVM, JS
Represents an array (specifically, a Java array when targeting the JVM platform). Array instances can be created using the arrayOf, arrayOfNulls and emptyArray standard library functions. See Kotlin language documentation for more information on arrays.
For Native
Represents an array. Array instances can be created using the constructor, arrayOf, arrayOfNulls and emptyArray standard library functions. See Kotlin language documentation for more information on arrays.
Constructors
Properties
size
Returns the number of elements in the array.
val size: IntFunctions
get
Returns the array element at the specified index. This method can be called using the index operator.
operator fun get(index: Int): TExtension Properties
lastIndex
Returns the last valid index for the array.
val <T> Array<out T>.lastIndex: IntExtension Functions
all
Returns true if all elements match the given predicate.
fun <T> Array<out T>.all(predicate: (T) -> Boolean): Booleanany
Returns true if array has at least one element.
fun <T> Array<out T>.any(): BooleanReturns true if at least one element matches the given predicate.
fun <T> Array<out T>.any(predicate: (T) -> Boolean): BooleanassociateBy
Returns a Map containing the elements from the given array indexed by the key returned from keySelector function applied to each element.
fun <T, K> Array<out T>.associateBy(
keySelector: (T) -> K
): Map<K, T>Returns a Map containing the values provided by valueTransform and indexed by keySelector functions applied to elements of the given array.
fun <T, K, V> Array<out T>.associateBy(
keySelector: (T) -> K,
valueTransform: (T) -> V
): Map<K, V>associateByTo
Populates and returns the destination mutable map with key-value pairs, where key is provided by the keySelector function applied to each element of the given array and value is the element itself.
fun <T, K, M : MutableMap<in K, in T>> Array<out T>.associateByTo(
destination: M,
keySelector: (T) -> K
): MPopulates and returns the destination mutable map with key-value pairs, where key is provided by the keySelector function and and value is provided by the valueTransform function applied to elements of the given array.
fun <T, K, V, M : MutableMap<in K, in V>> Array<out T>.associateByTo(
destination: M,
keySelector: (T) -> K,
valueTransform: (T) -> V
): MassociateTo
Populates and returns the destination mutable map with key-value pairs provided by transform function applied to each element of the given array.
fun <T, K, V, M : MutableMap<in K, in V>> Array<out T>.associateTo(
destination: M,
transform: (T) -> Pair<K, V>
): Maverage
Returns an average value of elements in the array.
fun Array<out Byte>.average(): Doublefun Array<out Short>.average(): Doublefun Array<out Int>.average(): Doublefun Array<out Long>.average(): Doublefun Array<out Float>.average(): Doublefun Array<out Double>.average(): DoublebinarySearch
Searches the array or the range of the array for the provided element using the binary search algorithm. The array is expected to be sorted according to the specified comparator, otherwise the result is undefined.
fun <T> Array<out T>.binarySearch(
element: T,
comparator: Comparator<in T>,
fromIndex: Int = 0,
toIndex: Int = size
): IntSearches the array or the range of the array for the provided element using the binary search algorithm. The array is expected to be sorted, otherwise the result is undefined.
fun <T> Array<out T>.binarySearch(
element: T,
fromIndex: Int = 0,
toIndex: Int = size
): Intcomponent1
Returns 1st element from the array.
operator fun <T> Array<out T>.component1(): Tcomponent2
Returns 2nd element from the array.
operator fun <T> Array<out T>.component2(): Tcomponent3
Returns 3rd element from the array.
operator fun <T> Array<out T>.component3(): Tcomponent4
Returns 4th element from the array.
operator fun <T> Array<out T>.component4(): Tcomponent5
Returns 5th element from the array.
operator fun <T> Array<out T>.component5(): Tcontains
Returns true if element is found in the array.
operator fun <T> Array<out T>.contains(element: T): Booleancount
Returns the number of elements in this array.
fun <T> Array<out T>.count(): IntReturns the number of elements matching the given predicate.
fun <T> Array<out T>.count(predicate: (T) -> Boolean): Intdistinct
Returns a list containing only distinct elements from the given array.
fun <T> Array<out T>.distinct(): List<T>elementAtOrElse
Returns an element at the given index or the result of calling the defaultValue function if the index is out of bounds of this array.
fun <T> Array<out T>.elementAtOrElse(
index: Int,
defaultValue: (Int) -> T
): TfilterIndexedTo
Appends all elements matching the given predicate to the given destination.
fun <T, C : MutableCollection<in T>> Array<out T>.filterIndexedTo(
destination: C,
predicate: (index: Int, T) -> Boolean
): CfilterIsInstance
Returns a list containing all elements that are instances of specified type parameter R.
fun <R> Array<*>.filterIsInstance(): List<R>filterIsInstanceTo
Appends all elements that are instances of specified type parameter R to the given destination.
fun <R, C : MutableCollection<in R>> Array<*>.filterIsInstanceTo(
destination: C
): CAppends all elements that are instances of specified class to the given destination.
fun <C : MutableCollection<in R>, R> Array<*>.filterIsInstanceTo(
destination: C,
klass: Class<R>
): CfilterNotNull
Returns a list containing all elements that are not null.
fun <T : Any> Array<out T?>.filterNotNull(): List<T>filterNotNullTo
Appends all elements that are not null to the given destination.
fun <C : MutableCollection<in T>, T : Any> Array<out T?>.filterNotNullTo(
destination: C
): CfilterNotTo
Appends all elements not matching the given predicate to the given destination.
fun <T, C : MutableCollection<in T>> Array<out T>.filterNotTo(
destination: C,
predicate: (T) -> Boolean
): CfilterTo
Appends all elements matching the given predicate to the given destination.
fun <T, C : MutableCollection<in T>> Array<out T>.filterTo(
destination: C,
predicate: (T) -> Boolean
): Cfind
Returns the first element matching the given predicate, or null if no such element was found.
fun <T> Array<out T>.find(predicate: (T) -> Boolean): T?findLast
Returns the last element matching the given predicate, or null if no such element was found.
fun <T> Array<out T>.findLast(predicate: (T) -> Boolean): T?first
Returns first element.
fun <T> Array<out T>.first(): TReturns the first element matching the given predicate.
fun <T> Array<out T>.first(predicate: (T) -> Boolean): TfirstOrNull
Returns the first element, or null if the array is empty.
fun <T> Array<out T>.firstOrNull(): T?Returns the first element matching the given predicate, or null if element was not found.
fun <T> Array<out T>.firstOrNull(
predicate: (T) -> Boolean
): T?flatMapTo
Appends all elements yielded from results of transform function being invoked on each element of original array, to the given destination.
fun <T, R, C : MutableCollection<in R>> Array<out T>.flatMapTo(
destination: C,
transform: (T) -> Iterable<R>
): Cflatten
Returns a single list of all elements from all arrays in the given array.
fun <T> Array<out Array<out T>>.flatten(): List<T>forEach
Performs the given action on each element.
fun <T> Array<out T>.forEach(action: (T) -> Unit)forEachIndexed
Performs the given action on each element, providing sequential index with the element.
fun <T> Array<out T>.forEachIndexed(
action: (index: Int, T) -> Unit)getOrElse
Returns an element at the given index or the result of calling the defaultValue function if the index is out of bounds of this array.
fun <T> Array<out T>.getOrElse(
index: Int,
defaultValue: (Int) -> T
): TgroupBy
Groups elements of the original array by the key returned by the given keySelector function applied to each element and returns a map where each group key is associated with a list of corresponding elements.
Groups values returned by the valueTransform function applied to each element of the original array by the key returned by the given keySelector function applied to the element and returns a map where each group key is associated with a list of corresponding values.
groupByTo
Groups elements of the original array by the key returned by the given keySelector function applied to each element and puts to the destination map each group key associated with a list of corresponding elements.
fun <T, K, M : MutableMap<in K, MutableList<T>>> Array<out T>.groupByTo(
destination: M,
keySelector: (T) -> K
): MGroups values returned by the valueTransform function applied to each element of the original array by the key returned by the given keySelector function applied to the element and puts to the destination map each group key associated with a list of corresponding values.
fun <T, K, V, M : MutableMap<in K, MutableList<V>>> Array<out T>.groupByTo(
destination: M,
keySelector: (T) -> K,
valueTransform: (T) -> V
): MgroupingBy
Creates a Grouping source from an array to be used later with one of group-and-fold operations using the specified keySelector function to extract a key from each element.
fun <T, K> Array<out T>.groupingBy(
keySelector: (T) -> K
): Grouping<T, K>indexOf
Returns first index of element, or -1 if the array does not contain element.
fun <T> Array<out T>.indexOf(element: T): IntindexOfFirst
Returns index of the first element matching the given predicate, or -1 if the array does not contain such element.
fun <T> Array<out T>.indexOfFirst(
predicate: (T) -> Boolean
): IntindexOfLast
Returns index of the last element matching the given predicate, or -1 if the array does not contain such element.
fun <T> Array<out T>.indexOfLast(
predicate: (T) -> Boolean
): IntisArrayOf
Checks if array can contain element of type T.
fun <T : Any> Array<*>.isArrayOf(): BooleanisEmpty
Returns true if the array is empty.
fun <T> Array<out T>.isEmpty(): BooleanisNotEmpty
Returns true if the array is not empty.
fun <T> Array<out T>.isNotEmpty(): BooleanisNullOrEmpty
Returns true if this nullable array is either null or empty.
fun Array<*>?.isNullOrEmpty(): BooleanjoinTo
Appends the string from all the elements separated using separator and using the given prefix and postfix if supplied.
fun <T, A : Appendable> Array<out T>.joinTo(
buffer: A,
separator: CharSequence = ", ",
prefix: CharSequence = "",
postfix: CharSequence = "",
limit: Int = -1,
truncated: CharSequence = "...",
transform: ((T) -> CharSequence)? = null
): AjoinToString
Creates a string from all the elements separated using separator and using the given prefix and postfix if supplied.
fun <T> Array<out T>.joinToString(
separator: CharSequence = ", ",
prefix: CharSequence = "",
postfix: CharSequence = "",
limit: Int = -1,
truncated: CharSequence = "...",
transform: ((T) -> CharSequence)? = null
): Stringlast
Returns the last element.
fun <T> Array<out T>.last(): TReturns the last element matching the given predicate.
fun <T> Array<out T>.last(predicate: (T) -> Boolean): TlastIndexOf
Returns last index of element, or -1 if the array does not contain element.
fun <T> Array<out T>.lastIndexOf(element: T): IntlastOrNull
Returns the last element, or null if the array is empty.
fun <T> Array<out T>.lastOrNull(): T?Returns the last element matching the given predicate, or null if no such element was found.
fun <T> Array<out T>.lastOrNull(
predicate: (T) -> Boolean
): T?mapIndexedNotNullTo
Applies the given transform function to each element and its index in the original array and appends only the non-null results to the given destination.
fun <T, R : Any, C : MutableCollection<in R>> Array<out T>.mapIndexedNotNullTo(
destination: C,
transform: (index: Int, T) -> R?
): CmapIndexedTo
Applies the given transform function to each element and its index in the original array and appends the results to the given destination.
fun <T, R, C : MutableCollection<in R>> Array<out T>.mapIndexedTo(
destination: C,
transform: (index: Int, T) -> R
): CmapNotNullTo
Applies the given transform function to each element in the original array and appends only the non-null results to the given destination.
fun <T, R : Any, C : MutableCollection<in R>> Array<out T>.mapNotNullTo(
destination: C,
transform: (T) -> R?
): CmapTo
Applies the given transform function to each element of the original array and appends the results to the given destination.
fun <T, R, C : MutableCollection<in R>> Array<out T>.mapTo(
destination: C,
transform: (T) -> R
): Cmax
Returns the largest element or null if there are no elements.
fun <T : Comparable<T>> any_array<T>.max(): T?maxBy
Returns the first element yielding the largest value of the given function or null if there are no elements.
fun <T, R : Comparable<R>> Array<out T>.maxBy(
selector: (T) -> R
): T?maxWith
Returns the first element having the largest value according to the provided comparator or null if there are no elements.
fun <T> Array<out T>.maxWith(
comparator: Comparator<in T>
): T?min
Returns the smallest element or null if there are no elements.
fun <T : Comparable<T>> any_array<T>.min(): T?minBy
Returns the first element yielding the smallest value of the given function or null if there are no elements.
fun <T, R : Comparable<R>> Array<out T>.minBy(
selector: (T) -> R
): T?minWith
Returns the first element having the smallest value according to the provided comparator or null if there are no elements.
fun <T> Array<out T>.minWith(
comparator: Comparator<in T>
): T?none
Returns true if the array has no elements.
fun <T> Array<out T>.none(): BooleanReturns true if no elements match the given predicate.
fun <T> Array<out T>.none(predicate: (T) -> Boolean): Booleanrandom
Returns a random element from this array.
fun <T> Array<out T>.random(): TReturns a random element from this array using the specified source of randomness.
fun <T> Array<out T>.random(random: Random): TrandomOrNull
Returns a random element from this array, or null if this array is empty.
fun <T> Array<out T>.randomOrNull(): T?Returns a random element from this array using the specified source of randomness, or null if this array is empty.
fun <T> Array<out T>.randomOrNull(random: Random): T?reduce
Accumulates value starting with the first element and applying operation from left to right to current accumulator value and each element.
fun <S, T : S> Array<out T>.reduce(
operation: (acc: S, T) -> S
): SreduceIndexed
Accumulates value starting with the first element and applying operation from left to right to current accumulator value and each element with its index in the original array.
fun <S, T : S> Array<out T>.reduceIndexed(
operation: (index: Int, acc: S, T) -> S
): SreduceOrNull
Accumulates value starting with the first element and applying operation from left to right to current accumulator value and each element. Returns null if the array is empty.
fun <S, T : S> Array<out T>.reduceOrNull(
operation: (acc: S, T) -> S
): S?reduceRight
Accumulates value starting with last element and applying operation from right to left to each element and current accumulator value.
fun <S, T : S> Array<out T>.reduceRight(
operation: (T, acc: S) -> S
): SreduceRightIndexed
Accumulates value starting with last element and applying operation from right to left to each element with its index in the original array and current accumulator value.
fun <S, T : S> Array<out T>.reduceRightIndexed(
operation: (index: Int, T, acc: S) -> S
): SreduceRightOrNull
Accumulates value starting with last element and applying operation from right to left to each element and current accumulator value. Returns null if the array is empty.
fun <S, T : S> Array<out T>.reduceRightOrNull(
operation: (T, acc: S) -> S
): S?requireNoNulls
Returns an original collection containing all the non-null elements, throwing an IllegalArgumentException if there are any null elements.
fun <T : Any> Array<T?>.requireNoNulls(): Array<T>reverse
Reverses elements in the array in-place.
fun <T> Array<T>.reverse()reversedArray
Returns an array with elements of this array in reversed order.
fun <T> Array<T>.reversedArray(): Array<T>scanIndexed
Returns a list containing successive accumulation values generated by applying operation from left to right to each element, its index in the original array and current accumulator value that starts with initial value.
fun <T, R> Array<out T>.scanIndexed(
initial: R,
operation: (index: Int, acc: R, T) -> R
): List<R>scanReduceIndexed
Returns a list containing successive accumulation values generated by applying operation from left to right to each element, its index in the original array and current accumulator value that starts with the first element of this array.
fun <S, T : S> Array<out T>.scanReduceIndexed(
operation: (index: Int, acc: S, T) -> S
): List<S>single
Returns the single element, or throws an exception if the array is empty or has more than one element.
fun <T> Array<out T>.single(): TReturns the single element matching the given predicate, or throws exception if there is no or more than one matching element.
fun <T> Array<out T>.single(predicate: (T) -> Boolean): TsingleOrNull
Returns single element, or null if the array is empty or has more than one element.
fun <T> Array<out T>.singleOrNull(): T?Returns the single element matching the given predicate, or null if element was not found or more than one element was found.
fun <T> Array<out T>.singleOrNull(
predicate: (T) -> Boolean
): T?sliceArray
Returns an array containing elements of this array at specified indices.
fun <T> Array<T>.sliceArray(
indices: Collection<Int>
): Array<T>sort
Sorts a range in the array in-place.
fun <T> Array<out T>.sort(
fromIndex: Int = 0,
toIndex: Int = size)Sorts the array in-place according to the order specified by the given comparison function.
fun <T> Array<out T>.sort(comparison: (a: T, b: T) -> Int)sortBy
Sorts elements in the array in-place according to natural sort order of the value returned by specified selector function.
fun <T, R : Comparable<R>> Array<out T>.sortBy(
selector: (T) -> R?)sortByDescending
Sorts elements in the array in-place descending according to natural sort order of the value returned by specified selector function.
fun <T, R : Comparable<R>> Array<out T>.sortByDescending(
selector: (T) -> R?)sortDescending
Sorts elements in the array in-place descending according to their natural sort order.
fun <T : Comparable<T>> Array<out T>.sortDescending()sorted
Returns a list of all elements sorted according to their natural sort order.
fun <T : Comparable<T>> Array<out T>.sorted(): List<T>sortedArray
Returns an array with all elements of this array sorted according to their natural sort order.
fun <T : Comparable<T>> Array<T>.sortedArray(): Array<T>sortedArrayDescending
Returns an array with all elements of this array sorted descending according to their natural sort order.
fun <T : Comparable<T>> Array<T>.sortedArrayDescending(): Array<T>sortedArrayWith
Returns an array with all elements of this array sorted according the specified comparator.
fun <T> Array<out T>.sortedArrayWith(
comparator: Comparator<in T>
): Array<out T>sortedBy
Returns a list of all elements sorted according to natural sort order of the value returned by specified selector function.
fun <T, R : Comparable<R>> Array<out T>.sortedBy(
selector: (T) -> R?
): List<T>sortedByDescending
Returns a list of all elements sorted descending according to natural sort order of the value returned by specified selector function.
fun <T, R : Comparable<R>> Array<out T>.sortedByDescending(
selector: (T) -> R?
): List<T>sortedDescending
Returns a list of all elements sorted descending according to their natural sort order.
fun <T : Comparable<T>> Array<out T>.sortedDescending(): List<T>sortedWith
Returns a list of all elements sorted according to the specified comparator.
fun <T> Array<out T>.sortedWith(
comparator: Comparator<in T>
): List<T>sortWith
Sorts the array in-place according to the order specified by the given comparator.
fun <T> Array<out T>.sortWith(comparator: Comparator<in T>)Sorts a range in the array in-place with the given comparator.
fun <T> any_array<T>.sortWith(
comparator: Comparator<in T>,
fromIndex: Int = 0,
toIndex: Int = size)subarrayContentToString
Returns a string representation of the contents of the subarray of the specified array as if it is List.
fun <T> Array<out T>.subarrayContentToString(
offset: Int,
length: Int
): Stringsum
Returns the sum of all elements in the array.
fun Array<out Byte>.sum(): Intfun Array<out Short>.sum(): Intfun Array<out Int>.sum(): Intfun Array<out Long>.sum(): Longfun Array<out Float>.sum(): Floatfun Array<out Double>.sum(): DoublesumBy
Returns the sum of all values produced by selector function applied to each element in the array.
fun <T> Array<out T>.sumBy(selector: (T) -> Int): IntsumByDouble
Returns the sum of all values produced by selector function applied to each element in the array.
fun <T> Array<out T>.sumByDouble(
selector: (T) -> Double
): DoubletoBooleanArray
Returns an array of Boolean containing all of the elements of this generic array.
fun Array<out Boolean>.toBooleanArray(): BooleanArraytoByteArray
Returns an array of Byte containing all of the elements of this generic array.
fun Array<out Byte>.toByteArray(): ByteArraytoCharArray
Returns an array of Char containing all of the elements of this generic array.
fun Array<out Char>.toCharArray(): CharArraytoCollection
Appends all elements to the given destination collection.
fun <T, C : MutableCollection<in T>> Array<out T>.toCollection(
destination: C
): CtoCStringArray
Convert this array of Kotlin strings to C array of C strings, allocating memory for the array and C strings with given AutofreeScope.
fun Array<String>.toCStringArray(
autofreeScope: AutofreeScope
): CPointer<CPointerVar<ByteVar>>toCValues
fun <T : CPointed> Array<CPointer<T>?>.toCValues(): CValues<CPointerVar<T>>toDoubleArray
Returns an array of Double containing all of the elements of this generic array.
fun Array<out Double>.toDoubleArray(): DoubleArraytoFloatArray
Returns an array of Float containing all of the elements of this generic array.
fun Array<out Float>.toFloatArray(): FloatArraytoIntArray
Returns an array of Int containing all of the elements of this generic array.
fun Array<out Int>.toIntArray(): IntArraytoLongArray
Returns an array of Long containing all of the elements of this generic array.
fun Array<out Long>.toLongArray(): LongArraytoMap
Returns a new map containing all key-value pairs from the given array of pairs.
Populates and returns the destination mutable map with key-value pairs from the given array of pairs.
fun <K, V, M : MutableMap<in K, in V>> Array<out Pair<K, V>>.toMap(
destination: M
): MtoMutableList
Returns a MutableList filled with all elements of this array.
fun <T> Array<out T>.toMutableList(): MutableList<T>toMutableSet
Returns a mutable set containing all distinct elements from the given array.
fun <T> Array<out T>.toMutableSet(): MutableSet<T>toShortArray
Returns an array of Short containing all of the elements of this generic array.
fun Array<out Short>.toShortArray(): ShortArraytoSortedSet
Returns a SortedSet of all elements.
fun <T : Comparable<T>> any_array<T>.toSortedSet(): SortedSet<T>toUByteArray
Returns an array of UByte containing all of the elements of this generic array.
fun Array<out UByte>.toUByteArray(): UByteArraytoULongArray
Returns an array of ULong containing all of the elements of this generic array.
fun Array<out ULong>.toULongArray(): ULongArraytoUShortArray
Returns an array of UShort containing all of the elements of this generic array.
fun Array<out UShort>.toUShortArray(): UShortArraywithIndex
Returns a lazy Iterable that wraps each element of the original array into an IndexedValue containing the index of that element and the element itself.
fun <T> Array<out T>.withIndex(): Iterable<IndexedValue<T>>zip
Returns a list of pairs built from the elements of this array and the other array with the same index.
The returned list has length of the shortest collection.
Returns a list of values built from the elements of this array and the other array with the same index
using the provided transform function applied to each pair of elements.
The returned list has length of the shortest collection.
fun <T, R, V> Array<out T>.zip(
other: Array<out R>,
transform: (a: T, b: R) -> V
): List<V>Returns a list of pairs built from the elements of this collection and other array with the same index.
The returned list has length of the shortest collection.