To check if an array is empty in Kotlin, you can use the isEmpty()
method.
In this example,
numbers
with some elements.isEmpty()
method and store the result in the variable isEmpty
.fun main() {
val numbers = intArrayOf(1, 2, 3)
val isEmpty = numbers.isEmpty()
println("Array is empty: $isEmpty")
}
Array is empty: false
In this example,
emptyArray
without any elements.isEmpty()
method and store the result in the variable isEmpty
.fun main() {
val emptyArray = intArrayOf()
val isEmpty = emptyArray.isEmpty()
println("Array is empty: $isEmpty")
}
Array is empty: true
In this tutorial, we learned How to check if an Array is Empty in Kotlin language with well detailed examples.