I have this bit of code for the Stack's pop method and i'm trying to figure out how it avoids loitering while still returning the element that our index is currently pointing to:
public String pop()
{ // Remove item from top of stack.
String item = a[--N];
a[N] = null; // Avoid loitering (see text).
if (N > 0 && N == a.length/4) resize(a.length/2);
return item;
}
From what i can understand we are pointing the reference item of our String object to the indexed element(we start from the last element using it's current size N-1 hence the decrements) of our array a. Then if we are returning the reference why are we setting that indexed element that our reference is pointing to null before doing so? Doesnt that make the item point to nothing and return nothing?