zoukankan      html  css  js  c++  java
  • Iterator迭代器

    集合的继承关系:

    Iterator迭代器是一个接口,无法直接使用,需要使用Iterator接口的实现类对象,获取该类的方法比较特殊
    遍历集合元素的两种方式:Iterator迭代器 增强for循环(foreach)一般的for循环(get(i)获取每一个元素)
    一:迭代器实现集合的遍历:
    package Exercise;
    
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.Iterator;
    
    /**
     * 2020/1/7
     * java.util.Iterator接口:迭代器(对集合元素进行遍历)
     * E next 取出集合中的下一个元素
     * boolean hasNext()判断集合中还有没有下一个元素
     * Iterator迭代器是一个接口,无法直接使用,需要使用Iterator接口的实现类对象,获取该类的方法比较特殊
     * Collection接口中有一个方法叫iterator(),返回的就是迭代器的实现类对象Iterator<E>  iterator
     *
     *   迭代器的使用步骤:
     *   1.使用集合中的方法iterator()获取迭代器的实现类对象,使用iterator接口接收(多态)
     *   2.hasNext()方法判断
     *   3.next()方法取出集合中的下一个元素
     */
    public class IteratorExample {
        public static void main(String[] args) {
            Collection<String> col=new ArrayList<>();
            col.add("zhang");
            col.add("zhao");
            col.add("wang");
            col.add("li");
            col.add("hui");
            //多态的具体应用:基类引用,引用不同的派生类对象  public interface Collection<E> extends Iterable<E>
            // 1.使用集合中的方法iterator()获取迭代器的实现类对象,使用iterator接口接收(多态)
            //接口                     实现类对象
            Iterator<String> iter = col.iterator();
    //        boolean b=iter.hasNext();
    //        System.out.println(b);
    //        String s=iter.next();
    //        System.out.println(s);//zhang
            //2.hasNext()方法判断
            //3.next()方法取出集合中的下一个元素
            while(iter.hasNext()){
                System.out.println(iter.next());
            }
        }
    }
    zhang
    zhao
    wang
    li
    hui

    具体函数分析:

    不能写成这样,会报java.util.NoSuchElementException的异常

    二:foreach循环(增强for循环)遍历集合数组:

     Collection<String> col=new ArrayList<>();
            col.add("zhang");
            col.add("zhao");
            col.add("wang");
            col.add("li");
            col.add("hui");
            for(String s:col){  //s相当于下标
                System.out.println(s);}

    用增强for循环遍历普通数组:

        public static void testFor(){
            String[] str=new String[]{"aa","bb","dd"};
            for(String s:str){
                System.out.print(s+" ");
            }
        }

    注: forEach循环的面试题,查看输出结果:

    public static void testFor(){
            String[] str=new String[]{"aa","bb","dd"};
            for(String s:str){
                System.out.print(s+" ");//aa bb dd
            }
            System.out.println();
        }
        public static void testFor2(){
            String[] str=new String[]{"aa","bb","dd"};
            for(int i=0;i<str.length;i++){
                str[i]=i+" ";//真正修改数组的值
            }
            System.out.println(Arrays.toString(str));//0 1 2
        }
        public static void testFor3(){
            String[] str=new String[]{"aa","bb","dd"};
            for(String s:str){
                s="AA"; //此处s是临时变量
                System.out.print(s+" ");//"AA AA AA""
            }
            System.out.println();
            System.out.println(Arrays.toString(str));//aa bb dd
        }

    三:用迭代器删除元素:

    ArrayList<Integer> arr=new ArrayList<>();
    arr.add(1);
    arr.add(2);
    arr.add(3);
    arr.add(4);
    Iterator<Integer> it=arr.iterator();
    while (it.hasNext()){
    int a=it.next();
    System.out.print(a+" ");
    if(a==2){
    it.remove();
    }
    }
    Iterator<Integer> it2=arr.iterator();
    while(it2.hasNext()){
    System.out.print(it2.next()+" ");
    }

     

    如果用ArrayList增加数据:

            ArrayList<Integer> arr=new ArrayList<>();
            arr.add(1);
            arr.add(2);
            arr.add(3);
            Iterator<Integer> it=arr.iterator();
            while (it.hasNext()){
                Integer a=it.next();
                System.out.print(a+" ");
                if(a==2){
                    arr.add(7);
                }
            }
            System.out.println();
            Iterator<Integer> it2=arr.iterator();
            while(it2.hasNext()){
                System.out.print(it2.next()+" ");
            }

    Exception in thread "main" java.util.ConcurrentModificationException

    用ArrayList删除:

    ArrayList<Integer> arr=new ArrayList<>();
            arr.add(1);
            arr.add(2);
            arr.add(3);
            arr.add(4);
            Iterator<Integer> it=arr.iterator();
            while (it.hasNext()){
                Integer a=it.next();
                System.out.print(a+" ");
                if(a == 2){ 
                    arr.remove(a);
                }
            }
            System.out.println();
            Iterator<Integer> it2=arr.iterator();
            while(it2.hasNext()){
                Integer a=it2.next();
                System.out.print(a+" ");
            }

    Exception in thread "main" java.util.ConcurrentModificationException

     注:如果要删除,不能删除的是倒数第二个元素;如果调用ArrayList对象的remove(),刚好删除的倒数第二个元素,那么因为找不到下一个元素,所以不会报出异常

  • 相关阅读:
    HDU 4565 So Easy!(数学+矩阵快速幂)(2013 ACM-ICPC长沙赛区全国邀请赛)
    HDU 4568 Hunter(最短路径+DP)(2013 ACM-ICPC长沙赛区全国邀请赛)
    URAL 1664 Pipeline Transportation(平面图最大流)
    HDU 1250 Hat's Fibonacci(高精度)
    HDU 1042 N!(高精度乘)
    算法模板の计算几何
    算法模板の数据结构
    算法模板の数学&数论
    算法模板之图论
    HDU 3260/POJ 3827 Facer is learning to swim(DP+搜索)(2009 Asia Ningbo Regional)
  • 原文地址:https://www.cnblogs.com/laurarararararara/p/12163586.html
Copyright © 2011-2022 走看看