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(),刚好删除的倒数第二个元素,那么因为找不到下一个元素,所以不会报出异常

posted @ 2020-01-07 20:31  acehm  阅读(213)  评论(0编辑  收藏  举报