Kotlin – 27 – Mutable Lists in Kotlin

In Kotlin, a mutable list is a type of list that allows you to modify its elements. Mutable lists are part of the Kotlin Standard Library and provide a flexible way to manage collections of data that can change over time. In this article, we will explore mutable lists in Kotlin, how to create them, manipulate their elements, and perform common operations.

Creating Mutable Lists

You can create a mutable list in Kotlin using the mutableListOf() function. This function initializes an empty mutable list or populates it with initial elements. Here’s an example of creating a mutable list of strings:


val mutableFruits: MutableList<String> = mutableListOf("apple", "banana", "cherry")

In this example, mutableFruits is a mutable list containing three strings. You can add, remove, or modify elements in this list as needed.

Adding Elements

To add elements to a mutable list, you can use the add() method. This method appends an element to the end of the list. Here’s an example:


mutableFruits.add("date")

After executing this code, the mutableFruits list will contain four elements, including “date” at the end.

Removing Elements

To remove elements from a mutable list, you can use methods such as remove() or removeAt(). The remove() method removes the first occurrence of a specified element, while removeAt() removes an element at a specific index. Here are examples of both methods:


mutableFruits.remove("banana") // Removes the first occurrence of "banana"
mutableFruits.removeAt(0) // Removes the element at index 0

After executing these commands, the list will be modified accordingly.

Updating Elements

You can update elements in a mutable list by assigning new values to specific indices. Here’s an example:


mutableFruits[1] = "blueberry" // Updates the element at index 1

This code changes the second element of the list from “banana” to “blueberry.”

Iterating Over Mutable Lists

To iterate over the elements of a mutable list, you can use a for loop or the forEach() function, just like with read-only lists. Here’s a for loop example:


for (fruit in mutableFruits) {
    println(fruit)
}

You can also use the forEach() function to perform actions on each element:


mutableFruits.forEach { fruit -> println("Found $fruit") }
Common List Operations

Mutable lists in Kotlin support various common operations, including:

  • size: Returns the number of elements in the list.
  • isEmpty(): Checks if the list is empty.
  • contains(): Checks if the list contains a specific element.
  • indexOf(): Finds the index of the first occurrence of an element.
  • lastIndexOf(): Finds the index of the last occurrence of an element.
  • clear(): Removes all elements from the list.

Here’s an example demonstrating some of these operations:


val fruitsSize = mutableFruits.size
val isEmpty = mutableFruits.isEmpty()
val hasBlueberry = mutableFruits.contains("blueberry")
val bananaIndex = mutableFruits.indexOf("banana")
val lastCherryIndex = mutableFruits.lastIndexOf("cherry")
mutableFruits.clear() // Removes all elements from the list
Conclusion

Mutable lists in Kotlin provide a powerful way to manage and manipulate collections of data that can change over time. They allow you to add, remove, and update elements, as well as perform various common list operations. Understanding how to work with mutable lists is essential for building dynamic and flexible Kotlin applications.