Iterator迭代器对象

目录:

》迭代器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);
}
原文地址:https://www.cnblogs.com/qq-757617012/p/4279835.html