Clear All Elements from ArrayList in Kotlin | Kotlin ArrayList clear Function

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

  1. Introduction
  2. clear Function Syntax
  3. Understanding clear
  4. Examples
    • Basic Usage
    • Clearing a Non-Empty List
  5. Real-World Use Case
  6. 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.

Comments