

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to add an item to an ArrayList in Kotlin?
In this example, we will see how we can define an ArrayList in Kotlin and add an item in the list. We can do it using the library function add() or we can use the "+=" operator. In order demonstrate, we will be creating two ArrayLists, one is of mutable type and the other is of immutable type.
Example – Inserting a new item using add()
We can insert an item to an ArrayList using the add() function provided by the Kotlin library. In this example, we will be creating two lists: one is "myMutableList" which is a collection of mutable data types, and the other one is "myImmutableList" which is a collection of immutable data types.
We cannot use add() directly on the immutable collection. In order to use the add() function, we need to first convert the immutable list to mutable by using the toMutableList() function and then we can apply add() in it.
fun main(args: Array<String>) { val myMutableList = mutableListOf(1, 2, 3) myMutableList.add(4) println("Example of mutable list: " + myMutableList) // Convert array to mutableList using toMutableList() method // Then, insert element into it var myImmutableList = listOf(1, 2, 3) myImmutableList.toMutableList().add(4) // myImmutableList is not a mutable List println("Example of Immutable list: " + myImmutableList) }
Output
It will produce the following output −
Example of mutable list: [1, 2, 3, 4] Example of Immutable list: [1, 2, 3]
Example – Adding an item using "+=" operator
Kotlin provides another operator "+=" to add an item into the list. This operator works on both mutable and immutable data types. We will modify the above example using "+=" operator.
fun main(args: Array<String>) { val myMutableList = mutableListOf(1, 2, 3) myMutableList += 4 println("Example of mutable list: " + myMutableList) var myImmutableList = listOf(1, 2, 3) myImmutableList += 4 println("Example of Immutable list: " + myImmutableList) }
Output
It will produce the following output −
Example of mutable list: [1, 2, 3, 4] Example of Immutable list: [1, 2, 3, 4]
- Related Questions & Answers
- How to remove an item from an ArrayList in Kotlin?
- How to add an item to an ArrayList in C#?
- How to add an item to a list in Kotlin?
- How to remove an item from an ArrayList in C#?
- How to insert an item in ArrayList in C#?
- How to convert an ArrayList to String in Kotlin?
- How to check if ArrayList contains an item in Java?
- How to pass an arrayList to another activity using intents in Android Kotlin?
- Add an element to specified index of ArrayList in Java
- How to reverse an ArrayList in Java?
- How to synchronize an ArrayList in Java?
- How to sort an ArrayList in C#?
- Add an object to the end of the ArrayList in C#
- How to replace an element of an ArrayList in Java?
- How to create an ArrayList from an Array in Java?