int k=nElems
Start at the end.
k > j
While we haven't reached the target position...
a[k] = a[k-1];
Move the current element up one position to the right.
k--
And move to the left.
So, shift all elements between the target position and the end one position to the right.
I thought arrays were immutable once created.
The array size is fixed once created - the element at any index can be modified or reassigned however you see fit.
What was probably done here - a larger array was created and nElems
was used to indicate the number of populated values - the remaining indices would just be null
. Wikipedia's article on dynamic arrays briefly mentions this - the concept of capacity versus size - the capacity would be a.length
, the size would be nElems
. This is identical to how an ArrayList
works.