Updated February 14, 2023
Introduction to Kotlin add to list
Kotlin add to list is used to add the elements to the list or the set; we are using add method for adding the list of elements. When adding elements, the specified elements will be appended to the end of a collection. Add all method is used to add every element of the object argument from the list of sets. This argument can be sequenced, iterable, or an array.
Key Takeaways
- We can also use the plus operator to add an item to the list. By using this operator, it will return the original list along with items.
- All the methods in the interface are supporting for read-only access. In addition, it contains the mutable list interface, which was used to modify the list.
Overview
We can add the element to the mutable list; the usual list in kotlin is immutable. Suppose we want to add an element to the given list, the list is the mutable list, or we need to convert the same into the mutable list and then add the element. We can use add (element) or add (index, element) functions to add the element to the mutable list. The add (element) function will add elements to the end of the mutable list. It is essential to add elements.
How to add a list in Kotlin?
We are using the add index function to add an element to the mutable list at the specified index into the kotlin.
The below steps show how we can add a list to kotlin. When adding the list element, first, we create the project name as kotlin_AddList.
Below steps shows how to create the application as follows:
1. For adding a list into the kotlin, in the first step, we create the project name as kotlin_AddList by using the Intellij idea; we are providing the below parameter while creating the kotlin project as follows.
Name – kotlin_AddList
location – \Documents
Language – Kotlin
Build system – Intellij
Jdk – Java version 11
Project – New project
2. After creating the project template, in this step, we check the project structure of the newly created project as follows.
3. After creating the project structure, in this step, we create the mutable list by using the integer values, and then we add the elements as 52 and the index as two by using the add (element, index). We are creating the file name as a list as follows.
Code:
fun main(args: Array<String>) {
val list = mutableListOf (2, 5, 7, 13, 19)
list.add (2, 52)
println (list)
}
Output:
4. If suppose index is greater than the size of the list, then add function will throw the java index out of bound exception as follows. In the below example, the index size is 4, and we are adding the element into the six positions.
Code:
fun main(args: Array<String>) {
val list = mutableListOf(3, 5, 11, 13, 19)
list.add(6, 52)
println (list)
}
Output:
Kotlin adds an element to list
A list is a collection of generic order elements which contain duplicate values. List into the kotlin is an interface extending the collection interface. The list is a collection where we store the same type of data in a single place. We can easily pass the single and multiple items into the kotlin list. We need a single mutable list for the same. We are using add function for adding a single element to the list. We are using the addAll method to add multiple values to the list.
There are two types of lists available in the kotlin as follows:
- Immutable list – The immutable list is the list we cannot modify. The nature of this list is read-only. Therefore, we cannot modify the list of immutable.
- Mutable list – We can modify the mutable list into the kotlin. We can modify the mutable list by using the add function.
The example below shows how we are adding the element to the list. In the below example, we are using add function, which was provided by the library class of kotlin. So in the example below, we are first adding blue and red to the list, then adding white and black to the list; we can see that both lists will be displayed in the output.
Code:
fun main (args: Array<String>) {
var addlist = arrayListOf("blue", "red")
println ("List: " + addlist)
addlist.add ("white")
addlist.add ("black")
println ("list: " + addlist)
}
Output:
Examples of Kotlin add to list
Different examples are mentioned below:
Example #1
In the first example, we add the element using the add method. We are adding the 43 elements after adding the list as follows.
Code:
fun main() {
val addlist: MutableList<Int> = mutableListOf (13, 21, 35)
addlist.add (43)
println (addlist)
}
Output:
Example #2
In the below example, we are adding the element by using the addall method as follows. We are adding the 45 and 52 elements after adding the list.
Code:
fun main() {
val addlist: MutableList<Int> = mutableListOf (13, 21, 35)
addlist.addAll (listOf (45, 52))
println (addlist)
}
Output:
FAQ of Kotlin add to list
Given below is the FAQ:
Q1. How do we use add function to add elements to the list?
Answer:
The below example shows how we can add method add elements into the list as follows.
Code:
fun main() {
val addlist: MutableList<Int> = mutableListOf (17, 23, 38)
addlist.add (47)
println (addlist)
}
Output:
Q2. Can we add multiple elements by using kotlin add to the list?
Answer:
We can add multiple elements to the list using the addall method in kotlin.
Q3. How many types of lists are available in it?
Answer:
There are two lists available in kotlin, i.e., immutable lists and mutable lists; we are mutable lists in kotlin.
Conclusion
We are using the add index function to add an element to the mutable list at the specified index into the kotlin. It is used to add the elements to the list or the set; we are using add method for adding the list of elements.
Recommended Articles
This is a guide to Kotlin add to list. Here we discuss the introduction and how to add a list in kotlin with Examples and FAQ. You may also have a look at the following articles to learn more –