I was learning emptyArray()
in Kotlin, but I can't assign values in it (which is for obvious) and neither I can set it's size. What is the use of emptyArray
in Kotlin?
There are cases where you want to fallback to an empty array or empty list. For example:
return someInputArray?.filterNotNull() ?: emptyArray()
In this case if I have a valid input array, I filter out null
values, otherwise if the source array was null
I return an empty array. Therefore an array is always returned instead of a null
. Empty lists and arrays are probably more common than passing around nullable lists or arrays in Kotlin.
So yes, it is empty and you cannot add to it, just as no JVM array is expandable once allocated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With