1. 程式人生 > >Java的Iterator叠代器

Java的Iterator叠代器

asn void addall set bsp main import integer spa

叠代器能夠將遍歷的操作與序列底層的結構分離

 1 import java.util.*;
 2 
 3 public class CrossContainerIterator {
 4 
 5     public static void main(String[] args) {
 6         
 7         ArrayList<Integer> arrayList = new ArrayList<Integer>(Arrays.asList(1,2,3));
 8         LinkedList<Integer> linkedList = new
LinkedList<Integer>(Arrays.asList(1,2,3)); 9 HashSet<Integer> hashSet = new HashSet<Integer>(); 10 11 hashSet.addAll(Arrays.asList(1,1,3)); 12 13 display(arrayList.iterator()); 14 display(linkedList.iterator()); 15 display(hashSet.iterator());
16 17 } 18 public static void display(Iterator<Integer> it) 19 { 20 while(it.hasNext()) 21 { 22 System.out.print(it.next()); 23 } 24 System.out.println(); 25 26 } 27 28 29 }

Java的Iterator叠代器