- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 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]
- Related Articles
- How to add an item to an ArrayList in Kotlin?
- How to add a list item in HTML?
- How to remove an item from an ArrayList in Kotlin?
- How to add an item to an ArrayList in C#?
- How to search for an item in a Lua List?
- Add a background color to the active list item in a Bootstrap list group
- Add badges component to Bootstrap list group item
- How to add accelerators to a menu item?
- How to check if an item exists in a C# list collection?
- How to initialize an empty array list in Kotlin?
- How to add a line break in an Android TextView using Kotlin?
- How to insert an item into a C# list by using an index?
- How to remove an item from a C# list by using an index?
- How to create a list in Kotlin?
- How to insert an item in a list at a given position in C#?

Advertisements