How to add an item to an ArrayList in Kotlin?

KotlinKotlin Android-Extensions

Kotlin Problem Overview


How to add an item to an ArrayList in Kotlin?

Kotlin Solutions


Solution 1 - Kotlin

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.

Solution 2 - Kotlin

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.

Solution 3 - Kotlin

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.

Solution 4 - Kotlin

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

private var songs: Array<String> = arrayOf()
   fun add(input: String) {
        songs += input
    }

Solution 5 - Kotlin

You can [add element to arrayList](https://tutorialwing.com/how-to-add-new-element-in-array-in-kotlin/#using-arraylist "Add Elements to ArrayList in Kotlin") using add() method in Kotlin. For example,

arrayList.add(10)

Above code will add element 10 to arrayList.

However, if you are using Array or List, then you can not add element. This is because Array and List are Immutable. If you want to add element, you will have to use MutableList.

Several workarounds:

  1. Using System.arraycopy() method: You can achieve same target by creating new array and copying all the data from existing array into new one along with new values. This way you will have new array with all desired values(Existing and New values)
  2. Using MutableList:: Convert array into mutableList using toMutableList() method. Then, add element into it.
  3. Using Array.copyof(): Somewhat similar as System.arraycopy() method.

Solution 6 - Kotlin

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

Solution 7 - Kotlin

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")

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRameshView Question on Stackoverflow
Solution 1 - KotlinTarun Deep AttriView Answer on Stackoverflow
Solution 2 - KotlinguenhterView Answer on Stackoverflow
Solution 3 - KotlinpokemzokView Answer on Stackoverflow
Solution 4 - KotlinThe MJView Answer on Stackoverflow
Solution 5 - KotlinTutorialwing.comView Answer on Stackoverflow
Solution 6 - Kotlinshekhar g hView Answer on Stackoverflow
Solution 7 - KotlinSiddharth KumarView Answer on Stackoverflow