Replacing and Removing Items

Overview

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.

Replacing Items in MyArrayList

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:

  1. First verify that the 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.
  2. Save the prior item from 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)
  3. Assign the item argument to position index in this.items
  4. Return the prior item that you saved to a new variable in Step 2
Testing

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.

Removing Items from MyArrayList

Sometimes, we want to remove an item without replacing it. For this, we will use the public T remove(int index) method, which should:

  1. First verify that the 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.
  2. Save the current item from 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)
  3. Shift all of the items in positions higher than index down one position
  4. Reduce the size instance variable of the ArrayList by 1
  5. Return the former item that you saved to a new variable in Step 2
Testing

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

Methods about the State of the MyArrayList

We have two final methods that we want to implement that are both related to the overall state of the array list.

  1. The first method public boolean isEmpty() should return whether or not the array list is currently empty (i.e., contains no items).
  2. The second method 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.
Testing

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.

Committing Your Progress

Don’t forget to frequently save your progress periodically to GitHub using the add, commit, and push commands with git.