How to initialize an empty array list in Kotlin?

SadeQ digitALLife picture SadeQ digitALLife · Jun 12, 2018 · Viewed 38.1k times · Source

I have an empty array list:

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?

Answer

LuCio picture LuCio · Jun 12, 2018

According to the api-doc:

val list = arrayListOf<Int>()

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