Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to add an item to a list in Kotlin?
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)
str.add("mango")
str.add("berry")
println("The modified list: " + str)
}
Output
On execution, it will yield the following output −
The original list: [orange, apple] The modified list: [orange, apple, mango, berry]
Advertisements
