Iterator Java

Iterator Java


Iterator Java Example – Explanation – Screenshot

Iteration is nothing but repeating the same operation multiple times. For this, every language, including Java, gives a control structure for loop. Especially for iterating the data structure elements, Java gives three special loops – Enumeration, Iterator and ListIterator. All the three are meant to iterate only; but they do the job with little variations and extra operations.

Let us write a small example on Iterator Java and then explain.


Iterator Java
Output Screenshot on Iterator Java

An ArrayList object al is created and added elements with add() method. iterator() method of ArrayList returns an object of Iterator interface containing all the elements of ArrayList. hasNext() method of Iterator iterate each element and next() method returns each element.

Fore more command over iterators, how they differ from each other, extra features added in ListIterator etc. are clear discussed in the following links of this same Web site.

Enumeration Iterator
ListIterator Arrays foreach
Collections foreach Enumeration vs Iterator
Iterator vs ListIterator

Leave a Reply

Your email address will not be published. Required fields are marked *