This article explores different ways to declare an empty array in Kotlin.

1. Using emptyArray() function

The standard approach in Kotlin to get an empty array of the specified type is using the emptyArray() function.

Download Code

2. Standard library function

Alternatively, we can use the Kotlin standard library function to generate an empty array. There are several functions to construct an array, each for respective data types – doubleArrayOf(), floatArrayOf(), longArrayOf(), intArrayOf(), charArrayOf(), shortArrayOf(), byteArrayOf(), and booleanArrayOf().

Download Code

 
To create a typed array, use the arrayOf<T>() function:

Download Code

3. Array constructor

We can also use the array constructor with the specified size 0 to create an empty array.

Download Code

 
To create a typed array, use the Array constructor as follows:

Download Code

 
We can even create an empty two-dimensional array containing non-empty arrays. For example, the following code creates a two-dimensional array of length 0, containing IntArray values:

Download Code

4. Using arrayOfNulls() function

Finally, we can use the arrayOfNulls() function to create an array of the specified size and the specified type, initialized with null.

Download Code

That’s all about declaring an empty array in Kotlin.