Arrays are containers with a fixed size. The emptyArray() function creates an array of length 0, and you really can't do much with this. You cannot store anything in it, and you cannot resize it.
A common use case in which you need an empty array will be the default value for the property, which will be set to another array at a later point. This might be better than storing null in this property by default, because you don't have to deal with the possible null value when your code uses an array. For example, you can safely iterate over an empty array.
For a simple example:
class NumberHolder { var numbers: Array<Int> = emptyArray() fun printNumbers() { numbers.forEach { println(it) } } }
Using this class will look something like this:
val nh = NumberHolder() nh.printNumbers() // prints nothing nh.numbers = arrayOf(1, 2, 3) nh.printNumbers() // prints 1, 2, and 3 on separate lines
source share