目錄:
》迭代器Iterator的使用
》迭代字符串集合
》迭代對象集合
》迭代器使用圖解,和原理分析
》java迭代器源代碼
》迭代器Iterator的使用:
》迭代字符串集合
        import java.util.ArrayList;import java.util.Collection;import java.util.Iterator;/** * 集合的迭代 *  * Iterator iterator() 集合的專用迭代器 *      E next()獲取指針當前的元素,指針移向下一個元素 *             NoSuchElementException 沒有這樣的元素,因為你已經找到了最后 *         boolean hasNext() 如果仍有元素可以迭代,則返回 true。 */public class IteratorDemo {    public static void main(String[] args) {        //創建集合        Collection c=new ArrayList();                //向集合中添加元素        c.add("hello");        c.add("world");        c.add("java");                //Iterator iterator()迭代器,集合的專用迭代方式        Iterator it=c.iterator();//Iterator是接口,iterator() 方法返回的是實現類,這里是多態        System.out.PRintln(it.next());        System.out.println(it.next());        System.out.println(it.next());//        System.out.println(it.next());//                        //        String s=null;//        while(it.hasNext()){//            s=(String)it.next();//            System.out.println(s);//        }    }}  》迭代對象集合
import java.util.ArrayList;import java.util.Collection;import java.util.Iterator;/** * * 練習:用集合存儲5個學生對象,并把學生進行遍歷,用迭代器遍歷。 * *問題1:能用while循環,那么能不能用for循環? *問題2:不要次使用it.next()方法,因為每次使用都是訪問一個對象。 */public class IteratorTest { public static void main(String[] args) { //創建集合對象 Collection c=new ArrayList(); //創建學生對象 Student s1=new Student("林清",26); Student s2=new Student("周潤發",45); Student s3=new Student("黃健翔",25); Student s4=new Student("謝霆鋒",30); Student s5=new Student("王菲",30); //向集合中添加學生對象 c.add(s1); c.add(s2); c.add(s3); c.add(s4); c.add(s5); //得到集合的迭代器 Iterator it=c.iterator(); //使用迭代器遍歷學生集合 Student s=null; while(it.hasNext()){ s=(Student)it.next(); System.out.println(s.getName()+"------"+s.getAge()); //NoSuchElementException 不要多次使用it.next()方法 // System.out.println(((Student)it.next()).getName()+"-----"+((Student)it.next()).getAge()); } //for循環改寫 /* Student s=null; for(Iterator it=c.iterator();it.hasNext();){ s=(Student)it.next(); System.out.println(s.getName()+"------"+s.getAge()); }*/ }}
//bean
      public class Student {    private String name;    private int age;            public Student() {        super();    }    public Student(String name, int age) {        super();        this.name = name;        this.age = age;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    }  
》迭代器使用圖解,和原理分析

》java迭代器源代碼
      public interface Inteator {    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 {//在實現類里面才有Iterator接口的具體實現    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();     //new Itr();while(it.hasNext()) {    String s = (String)it.next();    System.out.println(s);}  新聞熱點
疑難解答