zoukankan      html  css  js  c++  java
  • 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);
    }
  • 相关阅读:
    Coroutine in Java
    常见的开源日志(包括分布式)
    深入理解 Java G1 垃圾收集器--转
    卷积神经网络——本质上是在利用卷积做特征压缩,然后再全连接
    神经网络和反向传播算法——反向传播算法本质上是随机梯度下降,链式求导法则而来的
    LSTM入门学习——结合《LSTM模型》文章看
    LSTM入门学习——本质上就是比RNN的隐藏层公式稍微复杂了一点点而已
    LSTM模型
    syslog介绍-CS架构来采集系统日志
    NetFlow是一种数据交换方式,提供网络流量的会话级视图,记录下每个TCP/IP事务的信息
  • 原文地址:https://www.cnblogs.com/qq-757617012/p/4279835.html
Copyright © 2011-2022 走看看