WordList
which is an infinitely expanding container of Strings. The WordList
class will keep an array of Strings which starts at a size of 2 (the first array created is of size 2) but then doubles in size whenever the user of the class tries to add
an element which would not fit in the current wordlist. For example, the first time it doubles will be when trying to add the third word, then on the fifth, then on the ninth, and so on.Specifically,
WordList
will implement the following methods:add(String w)
: adds the stringw
to the word list, and expands the size of the underlying array if needed, by doubling its size. It returns the WordList.size()
: returns the number of words in the WordListlength()
: returns the length of the arraytoString()
: all the words, separated by spaces.
Furthermore, the
WordList
implements the following interface:public interface Iterator { /** * Resets the iterator to point to the first word. */ public void resetIterator(); /** * Returns the next word. If we are beyond the end then return null. * Also moves the iterator to the next word. * @return the next word or null. */ public String getNext(); /** * @return true if there are still words to iterate over. * false if we have reached the end */ public boolean hasNext(); }
For example, the following code:
public static void main(String[] args) { //Test adding words WordList l = new WordList(); System.out.println("There are " + l.size() + " words."); System.out.println("The words array is of length " + l.length() + "."); l.add("We").add("are").add("the").add("priests").add("of"); l.add("The").add("Temples").add("of").add("Syrinx"); System.out.println(l); System.out.println("There are " + l.size() + " words."); System.out.println("The words array is of length " + l.length() + "."); l.add("Our").add("great").add("computers").add("fill").add("the").add("hallowed").add("halls"); System.out.println(l); System.out.println("There are " + l.size() + " words."); System.out.println("The words array is of length " + l.length() + "."); //Test REALLY big word lists for (int i = 0; i < 10000000; i++) { l.add("Rush"); } System.out.println("There are " + l.size() + " words."); System.out.println("The words array is of length " + l.length() + "."); //Test the iterator System.out.println("--Testing the iterator"); l = new WordList(); l.add("We").add("have").add("assumed").add("control"); l.add("There").add("is").add("trouble").add("with").add("the").add("trees"); l.resetIterator(); while (l.hasNext()){ String w = l.getNext(); System.out.println(w); } }prints out
There are 0 words. The words array is of length 2. We are the priests of The Temples of Syrinx There are 9 words. The words array is of length 16. We are the priests of The Temples of Syrinx Our great computers fill the hallowed halls There are 16 words. The words array is of length 16. There are 10000016 words. The words array is of length 16777216. --Testing the iterator We have assumed control There is trouble with the trees
No comments:
Post a Comment