集合框架-迭代器使用圖解和原理解析

ZHOU_VIP發表於2017-04-19

迭代器的原始碼:


public interface Iterator {
	boolean hasNext();

	Object next();
}

public interface Iterable {
	Iterator iterator();
}

public interface Collection extends Iterable {
	Iterator iterator();
}

public interface List extends Collection {
	Iterator iterator();
}

public class ArrayList implements List {
	public Iterator iterator() {
		return new Itr();
	}

	private class Itr implements Iterator {
		public boolean hasNext() {
		}

		public Object next() {
		}
	}
}

Collection c = new ArrayList();
c.add("hello");
c.add("world");
c.add("java");

Iterator it = c.iterator();

while(it.hasNext())
{
	String s = (String) it.next();
	System.out.println(s);
}


相關文章