Untitled Document

Subscribe here to get updates on new Videos

 

24. Three Cursors of Java - Iterator ?

 

 

 

previous homenext

Iterator:

1. We can apply iterator concept for any Collection object hence it is universal cursor.
2. By using iterator we can perform both read and remove operations.We can create iterator object by using iterator() method of Collection Interface
public iterator iterator();


Example:


Iterator itr=C.Iterator();
Where C is any Collection Object.


Methods In Iterator

Iterator Interface defines the following three methods
1. public Boolean hasNext()
2. public Object next()
3. public void remove()


Limitations of Iterator:

1. By using Enumeration and Iterator we can move only towards forward direction and we can’t move to the backward direction, and hence these are single direction cursors.
2. By using Iterator we can perform only read and remove operatiions and we can’t perform replacement of new Objects.


Note:
To overcome above limitations of Iterator we should go for ListIterator.