File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Implementing java.util.Iterator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Implementing java.util.Iterator" Watch "Implementing java.util.Iterator" New topic
Author

Implementing java.util.Iterator

Garrett Smith
Ranch Hand

Joined: Jun 27, 2002
Posts: 401
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.

[/b]
<hr></blockquote>

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 ]

comp.lang.javascript FAQ: http://jibbering.com/faq/
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Try replacing the data declaration with:

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
Garrett Smith
Ranch Hand

Joined: Jun 27, 2002
Posts: 401
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:
Garrett Smith
Ranch Hand

Joined: Jun 27, 2002
Posts: 401
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Implementing java.util.Iterator