迭代器能够将遍历的操作与序列底层的结构分离

 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 }

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-15
  • 2022-01-29
  • 2021-10-22
  • 2021-08-15
猜你喜欢
  • 2021-08-27
  • 2021-06-02
  • 2021-05-29
  • 2021-08-21
  • 2022-12-23
相关资源
相似解决方案