Due by the end of Wednesday, January 31
addAll
method
in the class ArrayBag
(provided).
It is given a Collection
(a List or Set or possibly even a Bag)
and its job is to add every element from that Collection
to this Bag.
For example,
if this Bag starts with [A, B] in it,
and is asked to add everything from the Set
{E, T, A},
then it will end up with
[A, B, E, T, A] in it.
Its return value says whether anything was added to the Bag. (That is, did the number of things in the Bag change?)
Test your code using the TestAddAll program provided. You should see the following output:
That doesn't work for a List, where the order of the elements is important. We're going to take a look at how ArrayList's remove method might work.
Download the class TestRemove.
In it, write the method
int remove(String[] arr, int len, String toRemove)
that removes the first occurrence of toRemove
from arr
and moves all the following elements up
to fill in the hole.
(Remember to null out the location you moved from.)
len
says
how many of the elements of arr
were in use
(so like numInBag
in the ArrayBag class).
len
if toRemove
was found,
and the same as len
if it was not.
Run the TestRemove program. You should see the following output:
Your grade will be based on the following rubric:
Submit this/these files: