🎓 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 clear function in Kotlin is used to remove all elements from an ArrayList. This function is part of the Kotlin standard library and provides a convenient way to empty an ArrayList.
Table of Contents
- Introduction
clearFunction Syntax- Understanding
clear - Examples
- Basic Usage
- Clearing a Non-Empty List
- Real-World Use Case
- Conclusion
Introduction
The clear function allows you to remove all elements from an ArrayList, leaving it empty. This is useful for scenarios where you need to reset or reuse an ArrayList without creating a new instance.
clear Function Syntax
The syntax for the clear function is as follows:
fun <T> ArrayList<T>.clear()
Parameters:
- This function does not take any parameters.
Returns:
- This function does not return any value.
Understanding clear
The clear function removes all elements from the ArrayList, resulting in an empty list. The size of the list after calling clear will be 0.
Examples
Basic Usage
To demonstrate the basic usage of clear, we will create an ArrayList, add some elements, and then clear the list.
Example
fun main() {
val numbers = arrayListOf(1, 2, 3, 4, 5)
println("Before clear: $numbers")
numbers.clear()
println("After clear: $numbers")
}
Output:
Before clear: [1, 2, 3, 4, 5]
After clear: []
Clearing a Non-Empty List
This example shows how to clear a non-empty list and verify that it is empty afterward.
Example
fun main() {
val fruits = arrayListOf("Apple", "Banana", "Cherry")
println("Fruits before clear: $fruits")
fruits.clear()
println("Fruits after clear: $fruits")
println("Is the list empty? ${fruits.isEmpty()}")
}
Output:
Fruits before clear: [Apple, Banana, Cherry]
Fruits after clear: []
Is the list empty? true
Real-World Use Case
Resetting a List of Tasks
In real-world applications, the clear function can be used to reset a list of tasks, allowing you to start fresh without creating a new list instance.
Example
data class Task(val id: Int, val description: String)
fun main() {
val tasks = arrayListOf(
Task(1, "Do the laundry"),
Task(2, "Buy groceries"),
Task(3, "Write blog post")
)
println("Tasks before clear: $tasks")
tasks.clear()
println("Tasks after clear: $tasks")
}
Output:
Tasks before clear: [Task(id=1, description=Do the laundry), Task(id=2, description=Buy groceries), Task(id=3, description=Write blog post)]
Tasks after clear: []
Conclusion
The clear function in Kotlin is a simple and effective way to remove all elements from an ArrayList. It allows you to reset or reuse an ArrayList without creating a new instance, making it useful for various applications, including data processing, task management, and more.
By understanding and using the clear function, you can effectively manage and manipulate ArrayList collections in your Kotlin applications.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment