2014年5月7日 星期三

java iterators

Iterators

Q. What is an Iterable ?
A. Has a method that returns an Iterator.

public interface Iterable<Item>
{
    Iterator<Item> iterator();
}


Q. What is an Iterator ?
A. Has methods hasNext() and next().

public interface Iterator<Item>
{
    boolean hasNext();
    Item next();
    void remove();
}


Q. Why make data structures Iterable ?
A. Java supports elegant client code.

for (String s : stack){
    StdOut.println(s);
}







沒有留言:

張貼留言