CS-151 Labs > Lab 3. My ArrayList
Part 3. Completing MyArrayList
Now spend some time finishing up your implementation of MyArrayList
class. Don’t forget to write (and run!) your tests as you go along.
Public Methods
Add the implementation of the following methods to your MyArrayList
class or its JUnit test
class as appropriate.
E set(int index, E element)
- Change the value at the specified index to element. Return the previous
value. Throw an
IndexOutOfBoundsException
if the index is not within the allowed range. Note that this range is smaller than the range allowed foradd()
. void testSet()
- Store the numbers 0 through 20 in the
MyArrayList
usingadd()
, then useget()
andset()
to reverse the order of the elements. Iterate through the list and confirm that element i is equal to 20 - i. Add additional code to test theIndexOutofBoundsException
. E remove(int index)
- Remove the item at the specified index, shifting all subsequent elements to the
left. Return the item that was removed. Throw an
IndexOutOfBoundsException
if the index is not within the allowed range. Note that this range is smaller than the range allowed foradd()
. void testRemove()
- Insert the numbers 0 through 20, then remove every even-indexed entry,
starting at 0 (so, entries 0, 2, 4,…) and add them into a second
MyArrayList
. Check that your secondMyArrayList
contains entries 0, 2, 4, …, 20 (in order) and your originalMyArrayList
contains 1, 3, 5, …, 19) (in order). boolean isEmpty()
- Return
true
if there are no items stored,false
otherwise. void testIsEmpty()
- Test
isEmpty()
returnstrue
if your array list has no items, andfalse
otherwise. Try testing before you add an element, after, and then after you remove the element. void clear()
- Empty out the list. Be sure to allow garbage collection to take place by
setting array entries to
null
. void testClear()
- Insert multiple elements, then clear. Make sure your size is 0 and you cannot get any elements.