L04a

Due by the end of Wednesday, January 31

Starter Files:


SUBMIT   /   Submission Summary

Array Implementations

Activity 1
Complete the 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:

OK so far! [10, 20, 30, 40, 50, 40, 30] OK so far! [10, 20, 30, 40, 50, 40, 30]
Activity 2
In class we will see (or possibly have seen) that removing an element from the middle of a Bag is easy because we can move the last element of the array down to fill in the hole.

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

Run the TestRemove program. You should see the following output:

[These, are, my, words, and, my, words, are, these] [These, my, words, and, my, words, are, these, null] [These, words, and, my, words, are, these, null, null] [These, words, and, my, words, are, these, null, null] [These, words, and, my, words, are, null, null, null]
Submit your classes by the end of this recitation. (You do not need to submit your testing code; we know what your code is supposed to do!)

Your grade will be based on the following rubric:

Submit this/these files:


SUBMIT   /   Submission Summary