zoukankan      html  css  js  c++  java
  • iterator取集合元素

    1,完整代码

    //创建两个arraylist对象
            Collection al = new ArrayList();
            
            //al1添加元素
            al.add("name1");
            al.add("name4");
            al.add("name3");
            
            Iterator it=al.iterator();
            while(it.hasNext())
            {
                System.out.println(it.next());
            }

    for循环的实现

    for(Iterator it=al.iterator();it.hasNext();)
            {
                System.out.println(it.next());
            }

    for()的好处:更节约内存

    Iterator定义在了循环内部,在循环结束后,it就被释放了,

    而在While中it定义在了循环外面,循环结束后对象依然存在,但是却没什么用处,就造成了内存的浪费

    2,什么是迭代器?

    其实就是集合的取出方式。

    【通俗的讲:取一个在就判断一下集合中还有没有元素,有就取出,没有就结束】

    3,迭代器(Iterator)的方法?

    next();

    hasNext();

    remove();

    4,关键代码

    Iterator it=al.iterator();
            while(it.hasNext())
            {
                System.out.println(it.next());
            }

    5,迭代器与集合的关系?

    迭代器用于取出集合中的元素,各种集合的底层数据结构并不相同,所以存取方式是不同的,每个集合都具备取出的操作,但是集合的取出比较复杂,不止一个动作,就将取出这个动作封装成了一个对象

    定义在内部的原因:

    迭代器操作的是集合内部的元素,定义在类的内部更加方便

    如果创建在外部,还需要创建集合对象

    6,iterator实现源代码【为了防变代码的观看,我把代码反复到了自定义的.java文件中,导致的报错(忽略就好)】

    Iterator方法的最初定义是在AbstractList这个类中,他的方法实现很简单就是return了一个Itr对象,

    Itr是什么呢?

    在上图中可以看到,它是定义在AbstractList这个类里面的内部类

    在他的内部定义了我们经常使用的hasNext(),Next(),remove()

    然后在Iterator()里面返回了Itr对象

    7,下面是Itr的具体实现源码 

     private class Itr implements Iterator<E>
        {
        
            int cursor = 0;    
            int lastRet = -1;
            int expectedModCount = modCount;
        
            public boolean hasNext()
            {
                return cursor != size();
            }
    
            public E next() 
            {
                checkForComodification();
                try 
                {
                    E next = get(cursor);
                    lastRet = cursor++;
                    return next;
                } 
                catch (IndexOutOfBoundsException e)
                {
                    checkForComodification();
                    throw new NoSuchElementException();
                }
            }
        
            public void remove() {
                if (lastRet == -1)
                throw new IllegalStateException();
                    checkForComodification();
        
                try {
                AbstractList.this.remove(lastRet);
                if (lastRet < cursor)
                    cursor--;
                lastRet = -1;
                expectedModCount = modCount;
                } catch (IndexOutOfBoundsException e) {
                throw new ConcurrentModificationException();
                }
            }
        
            final void checkForComodification() 
            {
                if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
            }
        }
  • 相关阅读:
    CodeForces 288A Polo the Penguin and Strings (水题)
    CodeForces 289B Polo the Penguin and Matrix (数学,中位数)
    CodeForces 289A Polo the Penguin and Segments (水题)
    CodeForces 540C Ice Cave (BFS)
    网站后台模板
    雅图CAD
    mbps
    WCF学习-协议绑定
    数据库建表经验总结
    资源位置
  • 原文地址:https://www.cnblogs.com/excellencesy/p/8626551.html
Copyright © 2011-2022 走看看