insert(insertIndex, newElem)
Shift to the right the element currently at "insertIndex" (if any) and all elements to the right.

Valid "insertIndex" values are between 0 and "size". If insertIndex = "size" then it becomes a simple "add" operation.
insert(insertIndex, newElem)
Insertion Index Position
The index where we want to insert is indicated.
Shift
We need to shift all elements from the insertion position and beyond to the right to make room for insert.

NOTE -- before shifting, we want to check hasCapacity (and grow if necessary).
Empty Position For Insert
We now have an empty slot, ready for a new element.
Insert
With the blank slot now open, it's easy to insert the new element.
Algorithm
First try to come up with an algorithm on your own.

If you get stuck or want to compare then click to show a solution.


-- Recipe for "insert(index, newElement)" -- 1. If index is not valid (<0 or >size) throw exception 2. If !hasCapacity() grow() 3. shiftRightFrom(index) 4. elements[index] = newElement 5. size++ The logic for "shiftRightFrom" will use basic iteration and access operations. Hint: be careful not to "wipe out" existing data.