Methods That Remove Strings
In this lesson, we define the remaining methods for the class BagOfStrings, those that remove entries.
We have postponed the methods to remove strings from a bag until now because defining a method to remove a specific string is somewhat more difficult and involves a search like the one we performed in the method contains
. We begin, however, with the two methods that are not as difficult to define.
The method clear
The method clear
removes all the strings from a bag, one at a time. The following definition of clear
calls the method remove
until the bag is empty:
/** Removes all strings from this bag. */
public void clear()
{
while (!isEmpty())
remove();
} // End clear
Exactly which string is removed by each cycle of the loop is unimportant. Thus, we call the remove
method that removes an unspecified string. Moreover, we do not save the string that the method returns.
Checkpoint question
Revise the previous definition of the method clear
so that it does not call isEmpty
, and so the while
statement has an empty body. Hint: What does remove
return when the bag is empty?
// Write your definition of the method clear here:
We cannot test the method clear
yet, because we have not defined the method remove
.
But must clear
call remove
?
Checkpoint questions
Can you think of a better way to implement clear
, one that does not involve removing entries from the bag one at a time using the method remove
? Describe such an approach.
/**A class that partially implements a bag of strings by using an array.@author Frank M. Carrano*/public class BagOfStrings{private static final int DEFAULT_CAPACITY = 25;private String[] bag;private int numberOfStrings;/** Creates an empty bag whose capacity is 25. */public BagOfStrings(){this(DEFAULT_CAPACITY);} // End default constructor/** Creates an empty bag having a given capacity.@param capacity The integer capacity desired. */public BagOfStrings(int capacity){bag = new String[capacity];numberOfStrings = 0;} // End constructor// Define the method clear here:/** Removes all strings from this bag. */public void clear(){} // End clear/** Adds a new string to this bag.@param newString The string to be added.@return true if the addition is successful, or false if not. */public boolean add(String newString){boolean result = true;if (isFull()){result = false;}else{ // Assertion: result is true herebag[numberOfStrings] = newString;numberOfStrings++;} // End ifreturn result;} // End add/** Retrieves all strings that are in this bag.@return a newly allocated array of all the strings in this bag. */public String[] toArray(){String[] result = new String[numberOfStrings];for (int index = 0; index < numberOfStrings; index++){result[index] = bag[index];} // End forreturn result;// Note: The body of this method could consist of one return statement,// if you call Arrays.copyOf} // End toArray/** Sees whether this bag is full.@return true if this bag is full, or false if not */public boolean isFull(){return numberOfStrings == bag.length;} // End isFull/** Sees whether this bag is empty.@return true if this bag is empty, or false if not. */public boolean isEmpty(){return numberOfStrings == 0;} // End isEmpty} // End BagOfStrings
Removing an unspecified string
The method remove
that has no parameter removes any string from a bag, as long as the bag is not empty. Recall from its specification in the lesson Specifying a Bag that the method returns the string it removes. If the method cannot remove a string because the bag is empty, it returns null
.
Removing a string from a bag involves removing it from an array. While we can access any string in the array bag
, the last one is easy to remove. To do so, we:
- Get a reference to the string in
bag[numberOfStrings - 1]
so it can be returned - Set
bag[numberOfStrings - 1]
tonull
- Decrement
numberOfStrings
Decrementing numberOfStrings
causes the last entry to be ignored, meaning that ...