Kotlin Array - isNotEmpty() Function



The Kotlin array isNotEmpty() function is used to check whether the array is empty. It returns true if an array is not empty, otherwise, it returns false.

Syntax

Following is the syntax of Kotlin array isNotEmpty() function −

fun <T> Array<out T>.isNotEmpty(): Boolean

Parameters

This function does not accepts any parameters.

Return value

This function returns boolean value. true if an array is not empty. Otherwise; false.

Example 1

Following is the basic example to demonstrate the use of isNotEmpty() function −

fun main(args: Array<String>) {
   val array = arrayOf<Int>()
   // check array is empty or not
   val isempty = array.isNotEmpty()
   println("$isempty")
}

Output

On execution of the above code we get false because this array is empty −

false

Example 2

Now, let's see another example. Here, we created an array stores the strings. We then use isNotEmpty function to check array is empty or not −

fun main(args: Array<String>) {
   val array = arrayOf<String>("tutorialspoint", "India")
   // check array is empty or not
   val notempty = array.isNotEmpty()
   if(notempty == true){
      print("Array is not empty!")
   }else{
      print("Array is empty!")
   }
}

Output

After execution of the above code we get the following output −

Array is not empty!
kotlin_arrays.htm
Advertisements