What is Iterator ?

The iterator interface offers a general purpose,standardized way of accessing elements in a collection,one at a time. Iterator enables you to cycle through collection.Each Collection implements Iterater. So the elements of any collection can be accessed by Iterater methods.
The same code cycled through a List can be cycled for a Set also.
Example:
import java.util.*;
class demo
{
public static void main(String[] args){
ArrayList al=new ArrayList();
al.add("abcd");
al.add("kjhh");
al.add("kkhf");
al.add("cdfbg");
al.add("actfd");
Iterator it=al.iterator();
while(it.hasNext()){
System.out.println(it.next());
}}};

No comments:

Post a Comment