Comp 271 lab 0 - Lists of Strings

Goals

Overview

This lab deals with a class StrList, which is supposed to work a lot like ArrayList<String>. But some things need fixing.
   
The Strings themselves are stored in an array things[ ]. Not all of the array is necessarily used; only the first currsize slots are (things[0] through things[currsize-1]). The variable currsize thus represents the active length of the StrList not things.length. This way our StrList can easily grow in length, at least up until the hard limit of things.length. The main task of this lab is to grow past that point.

I've given you a starter/demo project to get started. It starts out with things.length = 5, and contains the following methods:

The zipped project is here.

You are to do the following:

Make add() work even when things[] is full

The trick here is to allocate a new array, say newnums, of larger size (doubling works, or increasing by 10, or increasing by 1.5 although that's trickier because of the floating-point conversions):
    String [] newthings = new String[newsize];
Next you copy from things[i] to newthings[i], for i<currsize. This is done with a straightforward for or while loop. Finally, you replace things by newthings:
    things = newthings
does it. Note that there is no need to do anything with the "old" things.

At this point, you now have space to add the new item, so you go ahead and do it. The overall add structure looks like:
    if (currsize == things.length) {
        // do the space expansion as above to ensure room
    }
    // now add the usual way; one way or another we know there is space!
    things[currsize] = y;
    currsize += 1;

Once you do this, the program should behave quite differently; you shouldn't get those "no more room to add ...." messages, and the entire list should print out.

Implement the reverse iterator

I've included an Iterator. An Iterator is technically a specific interface in Java; to implement this interface a class has to provide hasNext() and next() methods (and also remove()). The object implementing the Iterator interface must hold something internally about both the list and the current position in the list. Multiple iterators for the same list can exist at the same time, each with a different position.

The StrList iterator() method returns an object of type MyIterator; note that this class type is private, and is also an inner class; that is, it is defined within the StrList class. As an inner class to StrList, it has access to all the fields of StrList; as a private inner class, nobody in the outside world can see its internals, and because it implements Interface, the outside world knows what to expect of it.

You are to add a second iterator that iterates through the elements of the list in reverse order. I've created the private class for you, MyRevIterator, and also the riterator() method that creates and returns it. You need to finish MyRevIterator.next().

Use the reverse iterator

Modify the main program, WordTest, so that it uses the reverse iterator at some point to print out the list in reverse.

To submit your project, create a zipfile and email it to me at pld@cs.luc.edu.