java-IteratorDemo

来源:互联网 发布:淘宝卖家在哪里发微淘 编辑:程序博客网 时间:2024/06/08 05:55

关于集合框架迭代器及增强for()的用法

注意:for(Object 0 : 集合类型变量--表示相应集合)  一般只用来遍历集合,不做他用。

package com.raizofeeling.test;import java.util.ArrayList;import java.util.Collection;import java.util.Iterator;import java.util.LinkedList;/** * IteratorDemo.java description: 迭代器使用,删除、遍历,加强for()遍历 * @author raizoo * Created on 17-7-24 上午11:21. * @version 1.1 * @since JDK1.8 * @throws Exception: * */public class IteratorDemo {    public static void main(String[] args){        Collection collect = new ArrayList();        collect.add("111");        collect.add("222");        collect.add("333");        collect.add("444");        Iterator iterator = collect.iterator();        while(iterator.hasNext()){            String s = (String)iterator.next();            System.out.println(s);        }        /*         * Statement: for(Object o: 集合变量) 只用于遍历集合,一般不做它用         * 将集合collect中元素逐个赋给Object类型变量o,直到collect中元素取完为止.         * 特点:没有循环次数限制,直到集合遍历完为止.         *         */        for(Object o: collect){            System.out.println(o);        }    }}


原创粉丝点击