Java Iterator、ListIterator(迭代器)详解及代码示例

Java Iterator、ListIterator(迭代器)详解及代码示例

- 概念:
  • Iterator 是一种用于访问集合的方法,它不是集合。其可用来访问ArrayList、HashSet 等集合。
  • Iterator 是 Java 迭代器最简单的实现,ListIterator 是 Collection API 中的接口, 它扩展了 Iterator 接口。
  • Iterator 的常用方法如下:
方法 描述
boolean hasNext() 检测集合中是否还有元素。
E next() 返回迭代器的下一个元素,并且更新迭代器的状态。
void remove() 迭代器返回的元素删除。
  • ListIterator 的常用方法如下:
方法 描述
boolean hasNext() 检测集合中是否还有元素。
E next() 返回迭代器的下一个元素,并且更新迭代器的状态。
boolean hasPrevious() 检测当前迭代器所在位置之前是否还有元素。
E previous() 返回迭代器的上一个元素,并且更新迭代器的状态。
int nextIndex() 返回迭代器当前位置的下一个位置索引。
int previousIndex() 返回迭代器当前位置的上一个位置索引。
void remove() 迭代器返回的元素删除。
void set(E) 设置或修改迭代器当前所在位置的元素。
void add(E) 在当前迭代器位置后添加元素。
- 代码示例:
  • Iterator
//IteratorDemo.java
import java.util.ArrayList;
import java.util.Iterator;

public class IteratorDemo {
    
    

    public static void main(String[] args) {
    
    
        ArrayList<String> arrayList = new ArrayList<String>();
        arrayList.add("first");
        arrayList.add("second");
        arrayList.add("third");
        arrayList.add("fourth");
        arrayList.add("fifth");

        Iterator<String> it = arrayList.iterator();
        while(it.hasNext()){
    
    
            System.out.println("打印当前迭代器位置处的元素:"+it.next());
        }

    }

}


//运行结果如下:
//打印当前迭代器位置处的元素:first
//打印当前迭代器位置处的元素:second
//打印当前迭代器位置处的元素:third
//打印当前迭代器位置处的元素:fourth
//打印当前迭代器位置处的元素:fifth

//Process finished with exit code 0
//IteratorDemo.java
import java.util.ArrayList;
import java.util.Iterator;

public class IteratorDemo {
    
    

    public static void main(String[] args) {
    
    
        ArrayList<String> arrayList = new ArrayList<String>();
        arrayList.add("first");
        arrayList.add("second");
        arrayList.add("third");
        arrayList.add("fourth");
        arrayList.add("fifth");

        Iterator<String> it = arrayList.iterator();
        while(it.hasNext()){
    
    
            String removeEle = it.next();
            if("fourth".equals(removeEle)){
    
    
                it.remove();
            }
        }
        System.out.println("删除fourth这个元素后,arrayList的内容为:"+arrayList);

    }

}

//运行结果如下:
//删除fourth这个元素后,arrayList的内容为:[first, second, third, fifth]

//Process finished with exit code 0
  • ListIterator
package com.example.utils.collection;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;

public class ListIteratorDemo {
    
    

    public static void main(String[] args) {
    
    

        ArrayList<String> arrayList = new ArrayList<String>();
        arrayList.add("first");
        arrayList.add("second");
        arrayList.add("third");
        arrayList.add("fourth");
        arrayList.add("fifth");

        ListIterator<String> it = arrayList.listIterator();
        while(it.hasNext()){
    
    
            String removeEle = it.next();
            if("fourth".equals(removeEle)){
    
    
                it.set("updated fourth");
                it.add("sixth");
            }
        }
        System.out.println("执行上述操作后,arrayList的内容为:"+arrayList);

    }

}

//执行结果如下:
//执行上述操作后,arrayList的内容为:[first, second, third, updated fourth, sixth, fifth]

//Process finished with exit code 0

猜你喜欢

转载自blog.csdn.net/qq_38132105/article/details/125817025