123

How to add an item to an ArrayList in Kotlin?

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Ramesh
  • 1,772
  • 4
  • 12
  • 15

6 Answers6

154

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.

Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
Tarun Deep Attri
  • 8,174
  • 8
  • 41
  • 56
92

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.

Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
guenhter
  • 11,255
  • 3
  • 35
  • 66
  • 7
    doing `+=` 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
  • 1
    If 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
  • 1
    Exactly, 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
36

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.

pokemzok
  • 1,659
  • 1
  • 19
  • 29
10

You can add a new item to an array using +=, for example:

private var songs: Array<String> = arrayOf()
   fun add(input: String) {
        songs += input
    }
The MJ
  • 453
  • 7
  • 17
3

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")
Siddharth Kumar
  • 168
  • 1
  • 2
  • 13
0

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())`

shekhar g h
  • 1,193
  • 1
  • 9
  • 12