java-IteratorDemo

关于集合框架迭代器及增强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);
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值