< java.util >-- Iterator接口

每一个集合都有自己的数据结构,都有特定的取出自己内部元素的方式。为了便于操作所有的容器,取出元素。将容器内部的取出方式按照一个统一的规则向外提供,这个规则就是Iterator接口

也就说,只要通过该接口就可以取出Collection集合中的元素,至于每一个具体的容器依据自己的数据结构,如何实现的具体取出细节,这个不用关心,这样就降低了取出元素和具体集合的耦合性。

Iterator it = coll.iterator();//获取容器中的迭代器对象,至于这个对象是是什么不重要。这对象肯定符合一个规则Iterator接口。

-----------------------------------------------------------------------------

public static void main(String[] args) {

       Collection coll = new ArrayList();

       coll.add("abc0");

       coll.add("abc1");

       coll.add("abc2");

       //--------------方式1----------------------

       Iterator it = coll.iterator();

       while(it.hasNext()){

           System.out.println(it.next());

       }

       //---------------方式2用此种----------------------

       for(Iterator it = coll.iterator();it.hasNext(); ){

           System.out.println(it.next());

       }

    }

原文地址:https://www.cnblogs.com/wqing7/p/5896014.html