Kotlin – Check if Array is Empty
To check if an Array is empty in Kotlin language, use Array.isEmpty() method. Call isEmpty() method on this array, and the method returns True if the array is empty, or False if the array is not empty.
Syntax
The syntax to call isEmpty() method on Array arr
is
arr.isEmpty()
ADVERTISEMENT
Examples
In the following program, we take an empty array, and programmatically check if this array is empty or not using isEmpty() method.
Main.kt
fun main(args: Array<String>) { val arr = arrayOf<String>() if (arr.isEmpty()) { println("Array is empty.") } else { println("Array is not empty.") } }
Output
Array is empty.
Now, let us take an array with some elements, and check the result of isEmpty() method on this array.
Main.kt
fun main(args: Array<String>) { val arr = arrayOf("apple", "banana") if (arr.isEmpty()) { println("Array is empty.") } else { println("Array is not empty.") } }
Output
Array is not empty.
Conclusion
In this Kotlin Tutorial, we learned how to check if an array is empty or not, using Array.isEmpty() method, in Kotlin programming language with examples.