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
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.1

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.

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
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.1
Link copied to clipboard
operator fun iterator(): Iterator<T>

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

Since Kotlin 1.0
operator fun iterator(): Iterator<T>

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

Since Kotlin 1.1
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
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.1

Properties

Link copied to clipboard
val size: Int

Returns the number of elements in the array.

Since Kotlin 1.0
val size: Int

Returns the number of elements in the array.

Since Kotlin 1.1

Extensions

Link copied to clipboard
fun <T> Array<out T>.binarySearch(element: T, comparator: Comparator<in T>, fromIndex: Int = 0, toIndex: Int = size): Int
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
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
fun <R> Array<*>.filterIsInstance(klass: Class<R>): List<R>
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
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
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?
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?
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?
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?
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?
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?
fun <T> Array<out T>.minWith(comparator: Comparator<in T>): T?
Since Kotlin 1.0
Link copied to clipboard
fun <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)
fun <T> Array<out T>.sort(fromIndex: Int = 0, toIndex: Int = size)

Sorts a range in the array in-place.

Since Kotlin 1.0
fun <T> Array<out T>.sort(comparison: (a: T, b: T) -> Int)
fun <T> Array<out T>.sort(comparison: (a: T, b: T) -> Int)

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

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

Returns a new SortedSet of all elements.

Since Kotlin 1.0