How to initialize an empty array list in Kotlin?
ArraylistKotlinIndexoutofboundsexceptionArraylist Problem Overview
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?
Arraylist Solutions
Solution 1 - Arraylist
According to the api-doc:
val list = arrayListOf<Int>()
This is also mentioned here: How to initialize List
Solution 2 - Arraylist
I suggest you write
var myList: ArrayList<Int> = arrayListOf()