Hello, I have a basic assignment from the book "Just Java" By Peter van der Linden. The assignment is from Ch. 15, "Regular Expressions, Collections, Utilities". I'd like someone to check my work and see if it's ok.
Here is my solution.
There are no answers provided in the book, so no way for me to privately check my work. This code above compiles and runs ok, is it flawed in any way? [ March 16, 2003: Message edited by: Garrett Smith ]
Does the output make sense? Also, the hasNext() method should not change the state of the iterator at all - it should just report whether or not there's another element available. Only next() changes the state of the iterator. So it should be possible to iterate without ever calling hasNext(), like this:
This isn't recommended; I'm just saying it's possible. The following code should also work:
Also, after you fix the above problems - try the following code:
The first time through the iterator, we do three things: count the number of elements, print each one, and remove every second element. The next time through, we just print and count them all. The question is: when you remove an element, does the number of elements in Storage change? Most people would expect that it should, I think. If I have a collection with A, B, C, D, E (5 elements) and I remove the B and D, I would expect the collection to have A, C, E (3 elements) rather than A, null, C, null, E. This will require some extra thought and work. Note that it's also legal for a remove() method to simply throw UnsupportedOperationException. I don't have my copy of Just Java 2 handy, so I don't know the instructions - but it's possible that throwing the exception is entirely appropriate if there's not an actual need for someone to be able to use the remove() method.
"I'm not back." - Bill Harding, Twister
Joined: Jun 27, 2002
I have been trying to sqeeze in the time to write a proper remove method, but I don't really have it. After I get caught up with the books I already have I'm going to buy Data Structures in Java. I'm tempted to just throw UnsupportedOperationException for remove(). This doesn't work properly:
Joined: Jun 27, 2002
I finally got something to work.
It took me a while to figure out if(i-1 < newData.length) if I instead use i < newData.length, the code will only work when the backing array has an even number of elements. Thanks again, Jim. [ March 24, 2003: Message edited by: Garrett Smith ]