commons包提供了对数组的循环迭代功能,代码依赖jar包:commons-collections4-4.0-alpha1.jar
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.collections4.iterators.ArrayListIterator;
import org.apache.commons.collections4.iterators.UniqueFilterIterator;
//commons-collections4-4.0-alpha1.jar
public class CommonsIterator {
public static void main(String[] args) {
arrayIt();
uniqueIt();
}
/**数组迭代器*/
public static void arrayIt(){
System.out.println("===== 数组迭代器 ====");
int[] arr ={1,2,3,4,5};
Iterator<Integer> it =new ArrayListIterator<Integer>(arr);//1,2,3,4,5
//指定起始索引和结束索引
// Iterator<Integer> it =new ArrayListIterator<Integer>(arr,1,3);//2,3
while(it.hasNext()){
System.out.println(it.next());
}
}
/**去重迭代器*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void uniqueIt(){
System.out.println("=====去重迭代器 ====");
List<String> list =new ArrayList<String>();
list.add("a");
list.add("b");
list.add("a");
//去除重复的过滤器
Iterator<String> it =new UniqueFilterIterator(list.iterator());
while(it.hasNext()){
System.out.println(it.next());//a,b
}
}
}