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?
According to the api-doc:
val list = arrayListOf<Int>()
This is also mentioned here: How to initialize List in Kotlin? .
I suggest you write
var myList: ArrayList<Int> = arrayListOf()
val myList = arrayListOf<Int>() reads much better.val value= Array(20) { 0 }
setbutadd.. Also use the factory instead of constructor:val list = mutableListOf<Int>(). Also note I specifiedvalinstead ofvar(it has nothing to do with the list's mutability).