Friday, December 30, 2011

Iterator methods for Java

This is one of the most useful interface in java collection framework. It is similar to vector but it is unsynchronized .

Also it is similar to Enumeration in the Java collections framework. But it differ from enumerations in two ways

Iterators allows the caller to remove elements from the underlying collection during the iteration with well-defined semantics.

There are powerful methods for traversing elements.

Methods:

hasNext()
next()
remove()

Stpes:
1. First, obtain iterator to the start of the collection by calling the collection’s iterator( ) method
2. Then call the hasNext( ) method to find wethear nest item is available or not.
3. Finally, within the loop, obtain each element by calling next( ).

ListIterator

With help of this class traverse the elements in both ways, modify & adding new element in list during iteration, and obtain the iterator’s current position in the list.

Additional methods:

hasPrevious()
previous()
nextIndex()
previousIndex()

No comments:

Post a Comment