In this part of the lab, we will add the remaining functionality to our MyArrayList
class to replace and remove items from our data structure, as well as a few methods for affecting the current state of the data structure. We will also continue practicing using unit tests to debug and validate our implementation.
Sometimes when working with an array list, we want to replace an item that is currently stored so that the new item is now at the index of the former item. For this, we will use the public T set(int index, T item)
method, which should:
index
argument is valid, given the current number of items in the array list. That is, index
should be a value in the range [0, this.size - 1]
. If index
is not in this range, once again throw an IndexOutOfBoundsExeception
with an appropriate error message.this.items
at index
to a new variable so it can be returned after the replacement is finished (otherwise it will be forgotten in Step 3)item
argument to position index
in this.items
To verify that this method works properly, create another unit test called testSet
in your MyArrayListTest.java
file that creates a MyArrayList<Integer>
object, adds some items, replaces one of them, then checks (1) the correct item is now stored in the updated index, (2) none of the other items were affected, and (3) the correct prior item was returned.
You should also create a test called testSetExceptions
to verify whether the IndexOutOfBoundsExeception
is properly thrown when you try to use an index
that is too large and when the index
is negative. Don’t forget to use the Debugger to help you debug.
Sometimes, we want to remove an item without replacing it. For this, we will use the public T remove(int index)
method, which should:
index
argument is valid, given the current number of items in the array list. That is, index
should be a value in the range [0, this.size - 1]
. If index
is not in this range, once again throw an IndexOutOfBoundsExeception
with an appropriate error message.this.items
at index
to a new variable so it can be returned after the removal is finished (otherwise it will be forgotten in Step 3)index
down one positionsize
instance variable of the ArrayList
by 1To verify that this method works properly, once again create another unit test called testRemove
in your MyArrayListTest.java
file that creates a MyArrayList<Integer>
object, adds some items, then removes some of them and checks whether (1) none of the other items were affected (other than the appropriate ones moving to a new index), (2) the size
is correctly updated and (3) the correct former item was returned.
You should also create a test called testRemoveExceptions
to verify whether the IndexOutOfBoundsExeception
is properly thrown when you try to use an index
that is too large, including when we try to remove an item from an empty list, and when we use a negative index
.
We have two final methods that we want to implement that are both related to the overall state of the array list.
public boolean isEmpty()
should return whether or not the array list is currently empty (i.e., contains no items).public void clear()
should remove all of the items from the array (by assigning null
to any existing items) and reset the size
to 0.Once again, you should create unit tests in your MyArrayListTest.java
file that properly tests these two methods. To test isEmpty
in a unit test called testIsEmpty
, you could create two instances of MyArrayList<Integer>
– one with items and one without – then test whether isEmpty
returns the correct value using assertTrue
and assertFalse
.
To test clear
in a unit test called testClear
, you could create an instance of MyArrayList<Integer>
and add some items, then call clear
and check whether the size
becomes 0.
Don’t forget to frequently save your progress periodically to GitHub using the add
, commit
, and push
commands with git.