Careeroza
Creating Arrays
arrayOf and Array Constructor
An array holds a fixed number of values of the same type. Its size cannot change after creation.
Example
Create arrays from values, or with a size and a function that computes each element.
fun main() {
val cities = arrayOf("Delhi", "Mumbai", "Chennai")
val squares = Array(5) { i -> i * i }
val empty = arrayOfNulls<String>(3)
println(cities.contentToString())
println(squares.contentToString())
println(empty.contentToString())
println(cities.size)
}Output
[Delhi, Mumbai, Chennai] [0, 1, 4, 9, 16] [null, null, null] 3
Printing an Array
Printing an array directly, println(cities), shows something like [Ljava.lang.String;@1b6d3586 (type and memory id), not the values. Use contentToString() or joinToString().
Tip: Lists print nicely by default. That is one reason Kotlin code uses List much more often than arrays.