# Data structure removeat

**URL:** <https://forum.codewithmosh.com/t/data-structure-removeat/13696>\
**Category:** Data Structures\
**Created:** [July 14, 2022, 11:35pm UTC](https://forum.codewithmosh.com/t/data-structure-removeat/13696 "2022-07-14T23:35:30Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![jmrunkle](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/jmrunkle/32/3765_2.png) [@jmrunkle](https://forum.codewithmosh.com/u/jmrunkle)\
**Post date:** [July 16, 2022, 6:36pm UTC](https://forum.codewithmosh.com/t/data-structure-removeat/13696/4 "2022-07-16T18:36:56Z")

</div>

> 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.

```java
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.

---

_[View the full topic](https://forum.codewithmosh.com/t/data-structure-removeat/13696)._
