zoukankan      html  css  js  c++  java
  • Iterator使用和注意点

    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    
    public class IteratorTest {
    	public static void main(String[] args) {
    		List<Integer> l = new ArrayList<Integer>();
    		l.add(1);
    		l.add(3);
    		l.add(5);
    		l.add(3);
    		l.add(5);
    		l.add(87);
    	/*	List<String> a = new ArrayList<String>();
    		a.add("1");
    		a.add("2");
    		for (String item : a)//foreach循环集合元素不能改变
    		{//否则java.util.ConcurrentModificationException异常
    			if ("2".equals(item)) {
    				a.remove(item);
    			}
    		}
    		Iterator<String> ita = a.iterator();
    		while (ita.hasNext())
    		{
    			System.out.print(ita.next() + " ");
    		}*/
    		System.out.println();
    		Iterator<Integer> it = l.iterator();
    		while (it.hasNext())// 不要在 foreach 循环里进行元素的 remove/add 操作。 remove 元素请使用 Iterator方式
    		{
    			int t = it.next();
    			if (t == 5)
    				it.remove();
    			System.out.print(t +" ");
    		}
    		System.out.println();
    		System.out.println(l);
    	}
    }
    

    ========================================Talk is cheap, show me the code=======================================
    CSDN博客地址:https://blog.csdn.net/qq_34115899
  • 相关阅读:
    SQL exists( select 1 from
    svn不知道这样的主机
    SVN 操作指南
    SVN导出/导入、SVN备份/还原 【小白版】
    Asp.net窄屏页面 手机端新闻列表
    装饰者模式
    适配器模式
    原型模式
    建造者模式
    抽象工厂方法
  • 原文地址:https://www.cnblogs.com/lcy0515/p/9179831.html
Copyright © 2011-2022 走看看