FastList IndexOutOfBoundError even after calling ensureCapacity
younes-abouelnagah opened this issue · 1 comments
Scenario: Call ensureCapacity on a FastList then add to the list with a particular index.
Expected: success of all adds where the index is less than the capacity. Looking at the code of ensureCapacity supports this expectation since the method actually allocates a backing array of the requested capacity.
Current behavior: IndexOutOfBound can happen when adding at arbitrary indexes.
Reason: The add method with the index specified checks the index against the size rather than the capacity.
The implementation of add(index, object)
follows the contract defined in the Javadoc of java.util.List
.
Throws: IndexOutOfBoundsException - if the index is out of range (index < 0 || index > size())
The purpose of ensureCapacity() is to make sure that subsequent adding can be done without repeatedly resizing the backing array. It's an optimization only. It does not mutate the list in any visible way, like changing its size.