kotlin

Mutable List in Kotlin

Mutable lists are highly used as a collection in the Kotlin programming language. They can store multiple data items in it and you can also add or remove items from a mutable list.

Crearte a Mutable List

If you want to create a mutable list, you can use the mutableListof() function. You can provide the type of data that can be added to a mutable list. You can add or delete an item to the Mutable List using its built-in Kotlin functions. The mutable list is iterable, it means you can loop through it to access its items. You can also access the items of a mutable list using the item index. 

Syntax

interface MutableList<T> : List<T>, MutableCollection<T>

Create a mutable list - integer type items

val mList = mutableListOf<Int>(10, 20, 30, 40, 50, 60)
    
println(mList)

Output

[10, 20, 30, 40, 50, 60]

Create a mutable list - string type items

val names = mutableListOf<String>("Tony", "John", "Martin", "Krusten")
    
println(names)

Output

[Tony, John, Martin, Krusten]

Access Mutable List items

The items of a mutable list can be accessed using its index. You can access them using index and square brackets or you can also use the get() function for that and pass the item index.

val cars = mutableListOf<String>("TATA", "Nexa", "Tesla", "Volvo")
    
// access items using square brackets and index
println(cars[2])

// access items using get() function
println(cars.get(3))

Output

Tesla
Volvo

Add single item to a Mutable List

If you are using Mutable List in Kotlin, you can easily add new items to it. The mutable list has a function add() that takes the item as a parameter and adds that item to the list.

Syntax

MutableList.add(item)

Code Example

val numbers = mutableListOf<Int>(20, 25, 30, 35, 40, 45)
    
numbers.add(50)
    
println(numbers)

Output

[20, 25, 30, 35, 40, 45, 50]

Remove an item from the Mutable List

We can also remove items from a Mutable list using the index of the item. We can use the Mutable List function removeAt() to remove an item from the list. It takes the index of the item as a parameter and removes that element using the index.

Syntax

MutableList.removeAt(index)

Code Example

// create a mutable list
val students = mutableListOf<String>("John", "Kerry", "Carl", "Hawkins")

// remove item Carl from the list
students.removeAt(2)

// print the updated mutable list - students
println(students)

Output

[John, Kerry, Hawkins]

Iterate over a Mutable List

As we all know Mutable Lists in Kotlin are iterable items. It means you can loop through the items of the Mutable List and use the item in each iteration of the loop. We will be using a for loop here to iterate over the Mutable List.

// create a mutable list
val students = mutableListOf<String>("John", "Kerry", "Carl", "Hawkins")

for (student in students) {
    println(student)
}

Output

John
Kerry
Carl
Hawkins

Get the index of an item in a Mutable List

If you want to get the index of an item in a Mutable List you can use the function indexOf(). It will take the item as a parameter and return the index of the item in the list.

If the item does not exist in the list, it will return -1.

// create a mutable list
val students = mutableListOf<String>("John", "Kerry", "Carl", "Hawkins")

// get index of item - Kerry
val itemIndex = students.indexOf("Kerry")

// print index of the item
println(itemIndex)

Output

1
Was this helpful?