beyondgrader.com Logo
DemoBrowseAboutTeamLogin

SimpleArrayList add

Geoffrey Challen // 2020.10.0

Let's write the add method for our SimpleArrayList. First, create a SimpleArrayList class with a single public constructor that initializes the list with a passed array of Object references. You should assert that the passed array is not null. Next, implement a getter getValues() which returns the Object array that the list uses to store values. (This is purely for testing.) Also implement size() which returns the size of this list as an int.

Now write the add method, which takes the position to add at as an int as its first parameter and the Object reference to add as its second. add should add the element to the list, increasing the size by one and shifting elements after the add position backward. You should assert that the passed position is valid for this list. But note that you should allow adding a new item to the end of the existing list.

When you are done, here is how your SimpleArrayList class should work: