Create an empty Array in Kotlin
To create an empty Array in Kotlin, you can use arrayOf()
function. As we do not specify any initial elements, we have to specify the data type of elements that we would like to store in this Array.
For example, the syntax to create an empty Array of strings is
arrayOf<String>()
The syntax to create an empty Array of integers is
arrayOf<Int>()
Similarly, you can create an empty Array that can hold your required data type.
Examples
Create an empty Array of strings
In the following program, we create an empty Array of strings: fruitNames
using arrayOf() function.
Kotlin Program
fun main(args: Array<String>) {
val fruitNames = arrayOf<String>()
println(fruitNames.contentToString())
}
Output
[]