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