This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Note that the Iterator returned by this method is useless. It is already at the end, and cannot get to the start anymore. As such, every call to hasNext() will return false and you cannot get anything from it anymore.
If you really need the equals method, you can turn it round
. . . if ("null".equals(s)) . . .
Joined: Apr 29, 2009
null error is resolved..But,I see most of you commenting that the Iterator is not coded perfectly. Here's the requirement (an extra step to the initial requirement). Not sure how to proceed in the most efficient way..Can you please help?
Calling code- client
Actual method implementation
Joined: Apr 29, 2009
Can some one please suggest a solution. I am blank having tried this for a long time..
Joined: Oct 13, 2005
You will have to go through the API specifications for Iterator. Can you re-interpret that as meaning "if you have a List which contains null values this Iterator misses them out"? You will have to decide for yourself, but I would think yes, and the interpretation is like this:
You have created a subclass of a List class, maybe called ArrayListSkippingNull<E>
You write the documentation comments saying this class accepts null insertions, but its Iterator omits nulls when iterating.
You create a private inner class which implements the ListIterator<E> interface.
It has an index field which represents how many elements have been passed already (start = 0, end = n - 1).
Its hasNext() method counts forward, increaasing the index while the next reference is null, and returns true if you find a non-null reference before the end of the List.
Its hasPrevious method does the same in the other direction.
Its next method goes forward to find the next non-null reference (or throws a NoSuchElementException) incrementing the index if nulls are passed.
Its previous method does the same in the other direction.
Its remove method takes an element out of the List and moves all the other elements to fill in the gap and reduces the size of the List.
Your iterator and listIterator methods return an instance of that class.
I have never tried that, so I don't know whether it will work in your situation.