Adding to emptyArray


When creating an empty array:

var someArray = emptyArray<String>()

Is there anyway to add elements to it? I’ve tried someArray.set(0, 1) and always get an ArrayIndexOutOfBoundsException.

I’ve looked at the documentation and all and couldn’t find any answers. Do I have to predefine a size? If so, how do I go about doing that?


The size of an array is defined at its creation; an empty array will always remain empty. If you need a collection to which you can add elements, use ArrayList instead.


Thanks! It’s probably a stupid question, hopefully it helps other newcomers too.