How to Add Items to Mutable and Immutable ArrayList in Kotlin

Kailash Vaviya Feb 15, 2024
  1. Add Items to Kotlin ArrayList Using the add() Function
  2. Add Items to Kotlin ArrayList Using the += Operator
How to Add Items to Mutable and Immutable ArrayList in Kotlin

Kotlin ArrayList allows you to store and retrieve items from a collection in a specific order. Suppose you created a list of car brands for your program, but now, you want to add another item to the ArrayList.

The ArrayList class in Kotlin provides a dynamic array implementation. You can add new items to an existing ArrayList.

This makes it a useful data structure for situations where the number of items being stored is not known in advance or may change over time.

You can add new items to Kotlin ArrayList using a couple of ways, which are the following:

  1. add() method
  2. += operator

This article will go through both ways and write simple Kotlin programs to add new items to Kotlin ArrayList.

Add Items to Kotlin ArrayList Using the add() Function

The add() is a predefined method in the Kotlin library. We can add a new item to an ArrayList.

However, this method is available only if we work with a mutable ArrayList.

Here’s a program to demonstrate the use of the add() method with a mutable ArrayList. In this program, we will create an ArrayList named Cars and add one more brand.

Later, we will use the add() function to insert a new item into the list.

fun main(args: Array<String>) {
    val Cars = mutableListOf("Mercedes-Benz", "Ferrari", "BMW", "Bentley")
    println(Cars)

    Cars.add("Ford")
    println("New item added to ArrayList using the add() function: " + Cars)
}

Adding a new item to a Kotlin ArrayList using the add() function

If the ArrayList is immutable, we cannot add items. We will make the Cars ArrayList from the above code immutable and try to use the add() function with it.

fun main(args: Array<String>) {
    var Cars = listOf("Mercedes-Benz", "Ferrari", "BMW", "Bentley")
    println(Cars)

    Cars.add("Ford")
    println("New item added to ArrayList using the add() function: " + Cars)
}

Adding a new item to an immutable Kotlin ArrayList throws an error

As we see in the output, the program throws an error. Hence, in this case, we will first have to convert the immutable list to a mutable one using the toMutableList() function.

We can add items to the collection using the add() method alongside the toMutableList() function.

In this program, we will create the same immutable ArrayList Cars. But now, we will convert it to a mutable list before adding the new item.

fun main(args: Array<String>) {
    var Cars = listOf("Mercedes-Benz", "Ferrari", "BMW", "Bentley")
    println(Cars)

    Cars.toMutableList().add("Ford")

    println("New item added to ArrayList using the add() function: " + Cars)
}

Converting immutable Kotlin ArrayList to mutable for adding new items

The program will not throw an error this time as the list is converted to a mutable list.

Add Items to Kotlin ArrayList Using the += Operator

Besides the add() function, Kotlin also allows adding new items to an ArrayList using the += operator. Unlike the add() function, this operator works on mutable and immutable lists.

We will change the programs above and combine them to see how we can use the += operator to add items to Kotlin ArrayList.

fun main(args: Array<String>) {
    val mutableCars = mutableListOf("Mercedes-Benz", "Ferrari", "BMW", "Bentley")
    println(mutableCars)

    mutableCars += "Ford"
    println("New item added to ArrayList using the add() function: " + mutableCars + "\n")


    var immutableCars = listOf("Mercedes-Benz", "Ferrari", "BMW", "Bentley")
    println(immutableCars)

    immutableCars += "Ford"
    println("New item added to ArrayList using the add() function: " + immutableCars)
}

Adding new items to Kotlin ArrayList using the += operator

Kailash Vaviya avatar Kailash Vaviya avatar

Kailash Vaviya is a freelance writer who started writing in 2019 and has never stopped since then as he fell in love with it. He has a soft corner for technology and likes to read, learn, and write about it. His content is focused on providing information to help build a brand presence and gain engagement.

LinkedIn