# Got error in when removeAt() function in java

**URL:** <https://forum.codewithmosh.com/t/got-error-in-when-removeat-function-in-java/16066>\
**Category:** Data Structures\
**Created:** [October 26, 2022, 11:31am UTC](https://forum.codewithmosh.com/t/got-error-in-when-removeat-function-in-java/16066 "2022-10-26T11:31:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Harif07](https://avatars.discourse-cdn.com/v4/letter/h/53a042/32.png) [@Harif07](https://forum.codewithmosh.com/u/Harif07)\
**Post date:** [October 26, 2022, 11:31am UTC](https://forum.codewithmosh.com/t/got-error-in-when-removeat-function-in-java/16066/1 "2022-10-26T11:31:41Z")

</div>

Hello guys, I got error when Implement removeAt() function in arryas (ds&algo part1).  
I coded same as mosh how taught in video. But it got error like (index 6 out of bounds for length 6)  
Please give any solution for this.

 ![IMG_20221026_170012](https://us1.discourse-cdn.com/flex020/uploads/codewithmosh/original/2X/6/68c586225654a3d473afaa79ceae44df226b6243.jpeg)  
 ![IMG_20221026_165951](https://us1.discourse-cdn.com/flex020/uploads/codewithmosh/original/2X/9/92399da01792409cd315367414e30f9d121db667.jpeg)  
ngth 6)

---

<div class="post-metadata">

**Author:** ![UniqueNospaceShort](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/uniquenospaceshort/32/1814_2.png) [@UniqueNospaceShort](https://forum.codewithmosh.com/u/UniqueNospaceShort)\
**Post date:** [October 26, 2022, 7:50pm UTC](https://forum.codewithmosh.com/t/got-error-in-when-removeat-function-in-java/16066/2 "2022-10-26T19:50:49Z")

</div>

Hi,

are you sure this is the code given in the course?  
Try changing the condition in the for loop to `count-1`

| i | i+1 | count | Note |
| --- | --- | --- | --- |
| 2 | 3 | 6 | Item at index 3 crushes item at index 2 |
| 3 | 4 | 6 | Item at index 4 crushes item at index 3 |
| 4 | 5 | 6 | Item at index 5 crushes item at index 4. 5 is the greatest valid index in your table |
| 5 | 6 | 6 | At this point i+1 is out of bound. Hence the exception |
| 6 | 7 | 6 | Loop would not be executed because 6 is not strictly less than 6 |

Cheers.

---

<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:** [October 27, 2022, 2:11am UTC](https://forum.codewithmosh.com/t/got-error-in-when-removeat-function-in-java/16066/3 "2022-10-27T02:11:06Z")

</div>

> [@Data structure removeat](https://forum.codewithmosh.com/t/data-structure-removeat/13696/4):
>
> 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) { …

Previously answered. Please search the forums before asking questions.

---

<div class="post-metadata">

**Author:** ![Harif07](https://avatars.discourse-cdn.com/v4/letter/h/53a042/32.png) [@Harif07](https://forum.codewithmosh.com/u/Harif07)\
**Post date:** [October 28, 2022, 3:23am UTC](https://forum.codewithmosh.com/t/got-error-in-when-removeat-function-in-java/16066/4 "2022-10-28T03:23:31Z")

</div>

Yep, this is very frustrating.

---

<div class="post-metadata">

**Author:** ![Harif07](https://avatars.discourse-cdn.com/v4/letter/h/53a042/32.png) [@Harif07](https://forum.codewithmosh.com/u/Harif07)\
**Post date:** [October 28, 2022, 3:26am UTC](https://forum.codewithmosh.com/t/got-error-in-when-removeat-function-in-java/16066/5 "2022-10-28T03:26:22Z")

</div>

Yes, this code was given my mosh also taught this code in his tutorial. But first time it was worked. Then I don’t know what happened it’s not working properly. I know there is mistake in loop condition because it was out of bounds error. But I am beginner so it’s hard to me for debug this error.

---

<div class="post-metadata">

**Author:** ![Harif07](https://avatars.discourse-cdn.com/v4/letter/h/53a042/32.png) [@Harif07](https://forum.codewithmosh.com/u/Harif07)\
**Post date:** [October 28, 2022, 3:28am UTC](https://forum.codewithmosh.com/t/got-error-in-when-removeat-function-in-java/16066/6 "2022-10-28T03:28:41Z")

</div>

Hell yeah, it’s working dude thanks for your help uniqueNospaceShort your are saviour.
