🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
The isNotEmpty function in Kotlin is used to check whether an array is not empty. This function is part of the Kotlin standard library and provides a straightforward way to determine if an array contains any elements.
Table of Contents
- Introduction
isNotEmptyFunction Syntax- Understanding
isNotEmpty - Examples
- Basic Usage
- Using
isNotEmptywith Custom Types
- Real-World Use Case
- Conclusion
Introduction
The isNotEmpty function returns a boolean value indicating whether the array has at least one element. It is a convenient method for checking if an array is not empty.
isNotEmpty Function Syntax
The syntax for the isNotEmpty function is as follows:
fun isNotEmpty(): Boolean
Parameters:
- This function does not take any parameters.
Returns:
trueif the array is not empty;falseotherwise.
Understanding isNotEmpty
The isNotEmpty function is used to check if an array contains any elements. It is particularly useful in conditional statements where operations need to be performed only if the array is not empty.
Examples
Basic Usage
To demonstrate the basic usage of isNotEmpty, we will create an array of integers and check if it is not empty.
Example
fun main() {
val emptyArray = emptyArray<Int>()
val numbers = arrayOf(10, 20, 30, 40, 50)
println("Is the empty array not empty? ${emptyArray.isNotEmpty()}")
println("Is the numbers array not empty? ${numbers.isNotEmpty()}")
}
Output:
Is the empty array not empty? false
Is the numbers array not empty? true
Using isNotEmpty with Custom Types
This example shows how to use isNotEmpty to check if an array of custom objects is not empty.
Example
class Person(val name: String, val age: Int) {
override fun toString(): String {
return "Person(name='$name', age=$age)"
}
}
fun main() {
val emptyPeopleArray = emptyArray<Person>()
val people = arrayOf(
Person("Ravi", 25),
Person("Anjali", 30),
Person("Priya", 22)
)
println("Is the empty people array not empty? ${emptyPeopleArray.isNotEmpty()}")
println("Is the people array not empty? ${people.isNotEmpty()}")
}
Output:
Is the empty people array not empty? false
Is the people array not empty? true
Real-World Use Case
Performing Operations Based on Array Contents
In real-world applications, the isNotEmpty function can be used to perform operations only if the array contains elements, such as processing a list of tasks.
Example
fun main() {
val tasks = arrayOf("Task 1", "Task 2", "Task 3")
if (tasks.isNotEmpty()) {
tasks.forEach { task ->
println("Performing task: $task")
}
} else {
println("No tasks to perform.")
}
}
Output:
Performing task: Task 1
Performing task: Task 2
Performing task: Task 3
Conclusion
The Array.isNotEmpty function in Kotlin is a convenient method for checking if an array contains any elements. It is particularly useful for conditional operations that depend on whether an array is not empty. By understanding and using this function, you can effectively manage array checks in your Kotlin applications.
Comments
Post a Comment
Leave Comment