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());//hello
        }
        if (iterator.hasNext()){
            System.out.println(iterator.next());//无输出
        }*/
        //用while循环改进

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

    }
原文地址:https://www.cnblogs.com/luzhanshi/p/13061256.html