To declare an array in Kotlin, you can use the array creation functions like arrayOf()
, intArrayOf()
, or emptyArray()
.
In this example,
numbers
using intArrayOf()
function and initialize it with some values.fun main() {
val numbers = intArrayOf(1, 2, 3, 4, 5)
println("Array elements: ${numbers.joinToString()}")
}
Array elements: 1, 2, 3, 4, 5
In this example,
fruits
using arrayOf()
function and initialize it with some string values.fun main() {
val fruits = arrayOf("Apple", "Banana", "Orange")
println("Array elements: ${fruits.joinToString()}")
}
Array elements: Apple, Banana, Orange
In this tutorial, we learned How to Declare an Array in Kotlin language with well detailed examples.