Array

class Array<T>(source)

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.

Since Kotlin

1.0

Constructors

Link copied to clipboard
fun <T> Array(size: Int, init: (Int) -> T)

Creates a new array with the specified size, where each element is calculated by calling the specified init function.

Functions

Link copied to clipboard
operator fun get(index: Int): T

Returns the array element at the specified index. This method can be called using the index operator.

Since Kotlin 1.0
Link copied to clipboard
operator fun iterator(): Iterator<T>

Creates an Iterator for iterating over the elements of the array.

Since Kotlin 1.0
Link copied to clipboard
operator fun set(index: Int, value: T)

Sets the array element at the specified index to the specified value. This method can be called using the index operator.

Since Kotlin 1.0

Properties

Link copied to clipboard
val size: Int

Returns the number of elements in the array.

Since Kotlin 1.0

Extensions

Link copied to clipboard
actual fun <T> Array<out T>.asList(): List<T>

Returns a List that wraps the original array.

Since Kotlin 1.0
Link copied to clipboard
fun <T> Array<out T>.binarySearch(element: T, comparator: Comparator<in T>, fromIndex: Int = 0, toIndex: Int = size): Int

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.

Since Kotlin 1.0
fun <T> Array<out T>.binarySearch(element: T, fromIndex: Int = 0, toIndex: Int = size): Int

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, otherwise the result is undefined.

Since Kotlin 1.0
Link copied to clipboard
actual inline fun <T> Array<T>.copyOf(): Array<T>

Returns new array which is a copy of the original array.

Since Kotlin 1.0
actual inline fun <T> Array<T>.copyOf(newSize: Int): Array<T?>

Returns new array which is a copy of the original array, resized to the given newSize. The copy is either truncated or padded at the end with null values if necessary.

Since Kotlin 1.0
Link copied to clipboard
@JvmName(name = "copyOfRangeInline")
actual inline fun <T> Array<T>.copyOfRange(fromIndex: Int, toIndex: Int): Array<T>

Returns a new array which is a copy of the specified range of the original array.

Since Kotlin 1.0
Link copied to clipboard
actual inline fun <T> Array<out T>.elementAt(index: Int): T

Returns an element at the given index or throws an IndexOutOfBoundsException if the index is out of bounds of this array.

Since Kotlin 1.0
Link copied to clipboard
actual fun <T> Array<T>.fill(element: T, fromIndex: Int = 0, toIndex: Int = size)

Fills this array or its subrange with the specified element value.

Since Kotlin 1.0
Link copied to clipboard
fun <R> Array<*>.filterIsInstance(klass: Class<R>): List<R>

Returns a list containing all elements that are instances of specified class.

Since Kotlin 1.0
Link copied to clipboard
fun <C : MutableCollection<in R>, R> Array<*>.filterIsInstanceTo(destination: C, klass: Class<R>): C

Appends all elements that are instances of specified class to the given destination.

Since Kotlin 1.0
Link copied to clipboard
fun <T : Any> Array<*>.isArrayOf(): Boolean

Checks if array can contain element of type T.

Since Kotlin 1.0
Link copied to clipboard
fun <T : Comparable<T>> Array<out T>.max(): T?
Since Kotlin 1.0
Link copied to clipboard
inline fun <T, R : Comparable<R>> Array<out T>.maxBy(selector: (T) -> R): T?
Since Kotlin 1.0
Link copied to clipboard
fun <T> Array<out T>.maxWith(comparator: Comparator<in T>): T?
Since Kotlin 1.0
Link copied to clipboard
fun <T : Comparable<T>> Array<out T>.min(): T?
Since Kotlin 1.0
Link copied to clipboard
inline fun <T, R : Comparable<R>> Array<out T>.minBy(selector: (T) -> R): T?
Since Kotlin 1.0
Link copied to clipboard
fun <T> Array<out T>.minWith(comparator: Comparator<in T>): T?
Since Kotlin 1.0
Link copied to clipboard
actual inline fun <T> Array<out T>?.orEmpty(): Array<out T>

Returns the array if it's not null, or an empty array otherwise.

Since Kotlin 1.0
Link copied to clipboard
actual operator fun <T> Array<T>.plus(element: T): Array<T>

Returns an array containing all elements of the original array and then the given element.

Since Kotlin 1.0
actual operator fun <T> Array<T>.plus(elements: Collection<T>): Array<T>

Returns an array containing all elements of the original array and then all elements of the given elements collection.

Since Kotlin 1.0
actual operator fun <T> Array<T>.plus(elements: Array<out T>): Array<T>

Returns an array containing all elements of the original array and then all elements of the given elements array.

Since Kotlin 1.0
Link copied to clipboard
actual inline fun <T> Array<T>.plusElement(element: T): Array<T>

Returns an array containing all elements of the original array and then the given element.

Since Kotlin 1.0
Link copied to clipboard
actual inline fun <T : Comparable<T>> Array<out T>.sort()
fun <T> Array<out T>.sort()

Sorts the array in-place according to the natural order of its elements.

Since Kotlin 1.0
fun <T> Array<out T>.sort(fromIndex: Int = 0, toIndex: Int = size)

Sorts a range in the array in-place.

Since Kotlin 1.0
Link copied to clipboard
actual fun <T> Array<out T>.sortWith(comparator: Comparator<in T>)

Sorts the array in-place according to the order specified by the given comparator.

Since Kotlin 1.0
actual fun <T> Array<out T>.sortWith(comparator: Comparator<in T>, fromIndex: Int = 0, toIndex: Int = size)

Sorts a range in the array in-place with the given comparator.

Since Kotlin 1.0
Link copied to clipboard
fun <T> Array<out T>.toSortedSet(comparator: Comparator<in T>): SortedSet<T>

Returns a new SortedSet of all elements.

Since Kotlin 1.0