How to add an item to an ArrayList
in Kotlin?

- 27,862
- 20
- 113
- 121

- 1,772
- 4
- 12
- 15
6 Answers
For people just migrating from java
, In Kotlin
List
is by default immutable and mutable version of Lists is called MutableList
.
Hence if you have something like :
val list: List<String> = ArrayList()
In this case you will not get an add()
method as list is immutable. Hence you will have to declare a MutableList
as shown below :
val list: MutableList<String> = ArrayList()
Now you will see an add()
method and you can add elements to any list.

- 21,337
- 9
- 51
- 89

- 8,174
- 8
- 41
- 56
-
8Thank you! I've been banging my head over that plus method in Arrays... – dasfima Jul 16 '18 at 06:17
-
1Nice, just what I'm looking for. Thank you. – Sam Chen Aug 14 '20 at 02:33
-
What's the use difference between `val list: MutableList
= ArrayList()` and `val list = mutableListOf – Sam Chen Nov 14 '20 at 19:02()`? -
1Well actually ArrayList _is_ mutable since Kotlin 1.1: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-array-list/ – bk138 May 23 '21 at 16:24
If you have a MUTABLE collection:
val list = mutableListOf(1, 2, 3)
list += 4
If you have an IMMUTABLE collection:
var list = listOf(1, 2, 3)
list += 4
note that I use val
for the mutable list to emphasize that the object is always the same, but its content changes.
In case of the immutable list, you have to make it var
. A new object is created by the +=
operator with the additional value.

- 21,337
- 9
- 51
- 89

- 11,255
- 3
- 35
- 66
-
7doing `+=` in an non-mutable list, require to copy all elements of list in a new mutable-list and then reassign it to your actual list. I didn't know it was suitable declaring your list as `var`. anyway, declaring a list a var isn't a good idea for me. – crgarridos Sep 15 '17 at 09:17
-
1If a list is immutable, you have no other option as declaring it as `var`. If a immutable list is declared as `val`, than the list is not able to change ever. Just be reassigning it to another variable would be possible. – guenhter Sep 15 '17 at 09:43
-
1Exactly, I don't want to allow to change the reference of my variables. it can have a side effect in external/concurrency tasks. better to create a copy to extract the logic. But this is out of the scope of the question. – crgarridos Sep 15 '17 at 09:52
If you want to specifically use java ArrayList then you can do something like this:
fun initList(){
val list: ArrayList<String> = ArrayList()
list.add("text")
println(list)
}
Otherwise @guenhter answer is the one you are looking for.

- 1,659
- 1
- 19
- 29
You can add a new item to an array using +=
, for example:
private var songs: Array<String> = arrayOf()
fun add(input: String) {
songs += input
}

- 453
- 7
- 17
This is a code sample of how to add an item in a String
ArrayList
in Kotlin.
val arrayList: ArrayList<String> = ArrayList()
arrayList.add("January")

- 168
- 1
- 2
- 13
What I was doing was storing a list of strings in the viewmodel and accessing it in the fragment.
I created a dialog fragment and was editing the string in the dialog and returning the string value to the main fragment using navigation components.
When entering the dialog fragment the viewmodel was being cleared and the list with it. so when I wud pass the list to the adapter it would not really show anything and have a reference error.
Also, when passing list to adapter add ,toList()
`adapter.subMItList(viewModel.lableList.toList())`

- 1,193
- 1
- 9
- 12