Adding to emptyArray


#1

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?


#2

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.


#3

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