So I want to insert a value in a specific index, that mean I would throw exception if the index I want to insert is not exist or bigger than size, otherwise, I would use this method to ensure the capacity of the array is not null:
private void ensureCapacity(int size_wanted)
{
int max_capacity = storage.length;
if (size_wanted > max_capacity)
{
max_capacity = max_capacity * GROW_FACTOR +1;
storage = Arrays.copyOf(storage, max_capacity); // increases array size + copy contents
}
}
Then at this point, I suppose to extend the array and already made a copy of my original with the increased size already, right?
All I need to do is using a loop and shift everything to the right by one, to leave space for the index I want to insert at, correct?
>= sizeactually, remembering that indexes are 0-based.