Data structure removeat

There are only two hard things in Computer Science: cache invalidation, naming things, and off-by-one errors.

– Computer Scientists

Just got access to the course (decided to try the annual subscription at a discount) and you are correct. Mosh’s solution has an off-by-one error which is very common in programming. Instead of using i < count Mosh should have used i < count - 1 because you want to stop one before the last item.

public void removeAt(int index) {
  if (i < 0 || i >= count) {
    throw new IllegalArgumentException("Index out of bounds: " + index);
  }
  for (int i = index; i < count - 1; i++) {
    items[i] = items[i + 1];
  }
  count--;
}

Aside: this is why testing your code’s edge cases is important. I recommend Test-Driven Development to avoid this type of issue.