Reputation: 177
Basically, this code works:
var optionsList = ArrayList<String>()
optionsList.add("Alpha")
optionsList.add("Bravo")
optionsList.add("Charlie")
optionsList[2] = "Foxtrot"// Assignment works (only) if initialization has been done previously through add()
However, the following makes the application crash (with no compile time errors or warnings):
var optionsList = ArrayList<String>(3)
optionsList[0] = "Alpha"
optionsList[1] = "Bravo"
optionsList[2] = "Charlie"
This seems unexpected and I can't figure out the reason for the crash. In my understanding, both ways should be equivalent - in the first we are allocating memory for and initializing only one element at a time, whereas in the second we are allocating the memory for all elements together at the beginning and later changing values.
Can someone please help me understand what might be going on under the hood here? I have only just started learning Kotlin and have used C++ in the past, so that might be affecting how I am thinking about this.
Upvotes: 3
Views: 623
Reputation: 8096
In Kotlin list[n]
is a short hand for list.get(n)
, and with a assignment operator list[n] = value
it is translated to list.set(n, value)
.
The optionsList[2] = "Foxtrot"
tries to override the value at 2 and return the old value (that is never null) as defined in KDocs, so if there was no value at that index what will it return?
public E set(int index, E element) {
Objects.checkIndex(index, size);
E oldValue = elementData(index);
elementData[index] = element;
return oldValue;
}
It is true that the space is allocated in the memory for the values in
var optionsList = ArrayList<String>(3)
But since operator fun set(index: Int, element: E): E
requires to return the old value which is not possible because the value at that index never existed, so it fails to do the operation.
Edit: The point of allocation of space is to reduce the time taken to add a bulk of values, for instance if you wanted to add a large amount of values like 10000 then the pre-allocation (0.6ms) takes around 3.5x less time than dynamically allocated ArrayList (2ms). See: https://pl.kotl.in/iV3ZNNjOC
Upvotes: 2