Arrays

The generic class Array represents array of objects.

You can create instance by arrayOf() function or via constructor.

The arrayOfNulls() function can be used to create an array of a given size filled with null elements.

Methods get()/set() can be used via operator [].

The size property indicates size of array, in other words, the number of elements.

// i.e. ["0", "1", "4", "9", "16"]
val asc = Array(5) { i -> (i * i).toString() }
asc.forEach { println(it) }

vals asc2 = arrayOf("0", "1", "2", "3")

primitive type arrays

There are classes that represent arrays of primitive types without boxing overhead: ByteArray, ShortArray, IntArray, and so on.

These classes have no inheritance relation to the Array class, but they have the same set of methods and properties.

val x: IntArray = intArrayOf(1, 2, 3)
x[0] = x[1] + x[2]

// i.e [0, 0, 0, 0, 0]
val arr = IntArray(5)

// i.e. [42, 42, 42, 42, 42]
val arr = IntArray(5) { 42 }

// i.e [0, 1, 2, 3, 4] 
var arr = IntArray(5) { it * 1 }