class
Array<T> is an array (in other words, a collection of elements).
Any element can be gotten by integer index.
You cannot add or delete elements.
Kotlin
library kotlin-stdlib.jar the Array.kt file
class Array<T> {
public inline constructor(size: Int, init: (Int) -> T)
public val size: Int
public operator fun get(index: Int): T
public operator fun set(index: Int, value: T): Unit
public operator fun iterator(): Iterator<T>
}
To create
Array<T>, use the function:
Kotlin
fun arrayOf() : Array<T>
Kotlin
Example, create an Array
val myArray1:Array<String> = arrayOf("Book1", "Book2")
Very important! If you use primitives (
Int,
Long,
Float,
Double, etc.) in the
Array class like this:
Array<Int> or
Array<Long> or
Array<Double> or
Array<Float>
This affects performance because primitives reside in memory as objects. That is, the primitive is packed: memory is allocated for the primitive and the primitive is copied to this memory. Then this address is returned. This address is added to
Array.
Kotlin
// array
val my1:Array<Int> = arrayOf(10, 20, 30) // bad practis
val my2:IntArray = intArrayOf(10, 20, 30, 40, 50) // good practise
In
IntArray, all values are stored in one contiguous chunk of memory, with no references.