To initialize an array in Kotlin, you can use arrayOf() or Array constructor.
In this example,
numbers
using arrayOf()
with integer values.fun main() {
val numbers = arrayOf(1, 2, 3, 4, 5)
println("Array elements: ${numbers.joinToString()}")
}
Array elements: 1, 2, 3, 4, 5
In this example,
fruits
using the Array constructor with string values.fun main() {
val fruits = Array(3) { "Apple" }
println("Array elements: ${fruits.joinToString()}")
}
Array elements: Apple, Apple, Apple
In this tutorial, we learned How to Initialize an Array in Kotlin language with well detailed examples.