Search code examples
arraylistkotlinindexoutofboundsexception

How to initialize an empty ArrayList in Kotlin?


I have an empty arraylist:

var mylist: ArrayList<Int> = ArrayList()

When I want to set value in it I got this error:

java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0

The question is: How can I initialize my list?


Solution

  • According to the api-doc:

    val list = arrayListOf<Int>()
    

    This is also mentioned here: How to initialize List in Kotlin? .