I know that I cannot store a value at an index of an ArrayList that hasn't been used yet, i.e. is less than the size. In other words, if myArrayList.size() is 5, then if I try to do
myArrayList.set(10, "Hello World")
I will get an out of bounds error. But my app needs this. Other than a loop of storing null in each of the intermediate slots, is there a more elegant way?
It looks to me like:
- This behavior is the same in Vector
- If I need to be able to randomly access (i.e. element at pos X) then my choices are Vector and ArrayList.
- I could use a HashMap and use the index as a key but that's really inefficient.
So what is the elegant solution to what looks like a common case. I must be missing something...