十一、Java集和(三)——Iterator迭代器接口

1. Iterator迭代器接口

1)定义:

Iterator对象称为迭代器(设计模式的一种),主要用于遍历 Collection 集合中的元素。

2) 使用:
Collection c1 = new ArrayList();
c1.add(123);//自动装箱
c1.add(new Person("Tom",12));
c1.add(new String("AA"));
c1.add(new Date(234234324L));
c1.add("BB");
  • 获取迭代器
Iterator iterator = c1.iterator();//获取迭代器
  • 遍历
方式一(搞笑的)
System.out.println(iterator.next());
System.out.println(iterator.next());
System.out.println(iterator.next());
System.out.println(iterator.next());
System.out.println(iterator.next());

System.out.println(iterator.next());//当所有的元素都next完之后,再next会报NoSuchElementException
方式二(不推荐)
for(int i = 0;i < c1.size();i++){
	System.out.println(iterator.next());
}
方式三(推荐)
hasNext():判断集合的下个位置是否还有元素
while(iterator.hasNext()){
	//next():① 指针下移 ② 将下移以后位置上的元素返回
	Object obj = iterator.next();
	System.out.println(obj);
}
3)迭代器执行原理

迭代器执行原理

4)错误的遍历写法
Collection c1 = new ArrayList();
c1.add(123);//自动装箱
c1.add(new Person("Tom",12));
c1.add(new String("AA"));
c1.add(new Date(234234324L));
c1.add("BB");
//错误方式一:
Iterator iterator = c1.iterator();
while(iterator.next() != null){
	System.out.println(iterator.next());
	
}
//错误方式二:每次调用iterator()时,都会返回一个迭代器对象,指针从头开始
while(c1.iterator().hasNext()){
  	Object obj = c1.iterator().next();
	System.out.println(obj);
}
5)for-each循环的方式遍历集和(增强for循环)
Collection c1 = new ArrayList();
c1.add(123);//自动装箱
c1.add(new Person("Tom",12));
c1.add(new String("AA"));
c1.add(new Date(234234324L));
c1.add("BB");
//格式:for(集合元素类型  变量名 : 待遍历的集合对象的引用)
for(Object obj : c1){
	System.out.println(obj);
}
遍历数组
 @Test
public void test2(){
	int[] arr = {1,2,3,4,5};
	for(int i : arr){
		System.out.println(i);
	}
}

2. List集和特有Iterator

List接口提供了List特有的迭代器

List中的抽象方法
ListIterator<E> listIterator(int index);
ArrayList中实现的方法
public ListIterator<E> listIterator() {
    return new ListItr(0);
}

ListItr内部类: 可以通过ListItr在遍历的同时,实现add和set操作,可以向前或向后遍历

private class ListItr extends Itr implements ListIterator<E> {
    ListItr(int index) {
        super();
        cursor = index;
    }

    public boolean hasPrevious() {
        return cursor != 0;
    }

    public int nextIndex() {
        return cursor;
    }

    public int previousIndex() {
        return cursor - 1;
    }

    @SuppressWarnings("unchecked")
    public E previous() {
        checkForComodification();
        int i = cursor - 1;
        if (i < 0)
            throw new NoSuchElementException();
        Object[] elementData = ArrayList.this.elementData;
        if (i >= elementData.length)
            throw new ConcurrentModificationException();
        cursor = i;
        return (E) elementData[lastRet = i];
    }

    public void set(E e) {
        if (lastRet < 0)
            throw new IllegalStateException();
        checkForComodification();

        try {
            ArrayList.this.set(lastRet, e);
        } catch (IndexOutOfBoundsException ex) {
            throw new ConcurrentModificationException();
        }
    }

    public void add(E e) {
        checkForComodification();

        try {
            int i = cursor;
            ArrayList.this.add(i, e);
            cursor = i + 1;
            lastRet = -1;
            expectedModCount = modCount;
        } catch (IndexOutOfBoundsException ex) {
            throw new ConcurrentModificationException();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值