How to add an item to a list in Kotlin?

Kotlin Mobile DevelopmentApps/Applications

A List is a collection where you can store same type of data in one place. There are two types of lists in Kotlin −

  • An Immutable list is something that cannot be modified. It is read-only in nature.

  • The other type of list is mutable which can be modified.

In this article, we will see how to create a mutable list and how to add an item to the existing list.

Example – Adding an Item to a Mutable List

In order to add an item to a list, we will be using add() that is provided by the Kotlin library class.

fun main(args: Array<String>) {

   var str = arrayListOf("orange", "apple")
   println("The original list: " + str)


println("The modified list: " + str)


On execution, it will yield the following output −

The original list: [orange, apple]
The modified list: [orange, apple, mango, berry]
Updated on 01-Mar-2022 12:10:04