27.集合1------Collection

1.学习目录

 2.集合类体系结构

 

 3.Collection概述

 4.Collection方法

 5.Collection的遍历(Iterator)

    public static void main(String[] args) {
        Collection<String> collection = new ArrayList<String>();
        collection.add("hello");
        collection.add("world");
        Iterator<String> iterator = collection.iterator();
/**
 *     public Iterator<E> iterator() {
 *         return new Itr();
 *     }
 *
 *     private class Itr implements Iterator<E> {
 * ...
 *          }
 */
        /**
         System.out.println(iterator.next());//hello
         System.out.println(iterator.next());//world
         System.out.println(iterator.next());//Exception in thread "main" java.util.NoSuchElementException
         */
/*        if (iterator.hasNext()){
            System.out.println(iterator.next());//hello
        }
        if (iterator.hasNext()){
            System.out.println(iterator.next());//world
        }
        if (iterator.hasNext()){
            System.out.println(iterator.next());//无输出
        }*/
        //用while循环改进

        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }

    }

 

posted @ 2020-06-07 17:16  指尖下的世界  阅读(161)  评论(0编辑  收藏  举报
/* 看板娘 */ /*炸泡*/
/* 鼠标点击求赞文字特效 */