# A bug in the implementation of the RemoveAt() method of the Array class

**URL:** <https://forum.codewithmosh.com/t/a-bug-in-the-implementation-of-the-removeat-method-of-the-array-class/831>\
**Category:** Data Structures\
**Created:** [November 22, 2020, 3:40pm UTC](https://forum.codewithmosh.com/t/a-bug-in-the-implementation-of-the-removeat-method-of-the-array-class/831 "2020-11-22T15:40:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![shirelm16](https://avatars.discourse-cdn.com/v4/letter/s/35a633/32.png) [@shirelm16](https://forum.codewithmosh.com/u/shirelm16)\
**Post date:** [November 22, 2020, 3:40pm UTC](https://forum.codewithmosh.com/t/a-bug-in-the-implementation-of-the-removeat-method-of-the-array-class/831/1 "2020-11-22T15:40:06Z")

</div>

![image](https://us1.discourse-cdn.com/flex020/uploads/codewithmosh/original/1X/a6da7b3e8a28275edb81fa0f1fd40b27f53c0db8.jpeg)

But it seems like there’s a tiny little bug in the implementation of the RemoveAt() method. I believe the for-loop condition should be **i \< count - 1** and not **i \< count**.  
This scenario will lead to an IndexOutOfRange exception:

_Array array = new Array(3);_  
_array.Insert(10);_  
_array.Insert(20);_  
_array.Insert(30);_  
_array.RemoveAt(1);_  
_array.Print();_

---

<div class="post-metadata">

**Author:** ![dimanjenje](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/dimanjenje/32/331_2.png) [@dimanjenje](https://forum.codewithmosh.com/u/dimanjenje)\
**Post date:** [December 9, 2020, 7:13am UTC](https://forum.codewithmosh.com/t/a-bug-in-the-implementation-of-the-removeat-method-of-the-array-class/831/2 "2020-12-09T07:13:11Z")

</div>

> [@shirelm16](#):
>
> **i \< count - 1** and not **i \< count**

Actually you’re right, I was facing OutOfRange exception, even my solution (with C#), was the same as in the course, so a change with your sugestion and it pass the test.  
Thank you!

---

<div class="post-metadata">

**Author:** ![Raidenbond](https://avatars.discourse-cdn.com/v4/letter/r/b487fb/32.png) [@Raidenbond](https://forum.codewithmosh.com/u/Raidenbond)\
**Post date:** [December 31, 2020, 7:39pm UTC](https://forum.codewithmosh.com/t/a-bug-in-the-implementation-of-the-removeat-method-of-the-array-class/831/3 "2020-12-31T19:39:30Z")

</div>

@moshhamedani hey mosh and how are you and thanks for courses and I am progressing, I want to ask you about the data structure course , in the Arrays , how do u connect public static void main(){ Array numbers = new Array(length:3) } from where you declared from other functions Array.java from the lession 4 to lession 5 ? When u set initation private int[] length? And if i declare length:3 that colon : gives me an error!

Any help will do

Thanks!

Jason Ma

---

<div class="post-metadata">

**Author:** ![SAM](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/sam/32/167_2.png) [@SAM](https://forum.codewithmosh.com/u/SAM)\
**Post date:** [January 1, 2021, 11:44am UTC](https://forum.codewithmosh.com/t/a-bug-in-the-implementation-of-the-removeat-method-of-the-array-class/831/4 "2021-01-01T11:44:49Z")

</div>

> [@Raidenbond](#):
>
> And if i declare length:3 that colon : gives me an error!

> [@(length:3); error](https://forum.codewithmosh.com/t/length-3-error/543/2):
>
> Don’t type the parameter names when passing arguments to constructors or functions. So instead of new Array(length:3) just type new Array(3).

---

<div class="post-metadata">

**Author:** ![Raidenbond](https://avatars.discourse-cdn.com/v4/letter/r/b487fb/32.png) [@Raidenbond](https://forum.codewithmosh.com/u/Raidenbond)\
**Post date:** [January 1, 2021, 6:58pm UTC](https://forum.codewithmosh.com/t/a-bug-in-the-implementation-of-the-removeat-method-of-the-array-class/831/5 "2021-01-01T18:58:10Z")

</div>

Thank you for responding chung ,

One question is do i need to tie main.java and array.java together in way so array can run main method or makes sense for me to put main method to run to the program in one file when put public static vois main (string [] args ) into the array.java api

Thanks again happy new year

---

<div class="post-metadata">

**Author:** ![Raidenbond](https://avatars.discourse-cdn.com/v4/letter/r/b487fb/32.png) [@Raidenbond](https://forum.codewithmosh.com/u/Raidenbond)\
**Post date:** [January 1, 2021, 9:11pm UTC](https://forum.codewithmosh.com/t/a-bug-in-the-implementation-of-the-removeat-method-of-the-array-class/831/6 "2021-01-01T21:11:04Z")

</div>

And also on top of that hwo do u define insert ? When state the array when u define numbers.insert(5) say the index is 4 . The insert gives me error? Is it part of Java library api?

Thanks again!!!
