曦~妍  
 1 package jihe;
 2 
 3 import java.util.*;
 4 
 5 public class IteratorDemo {
 6     /*在项目中新建IteratorDemo类,在类中创建一个ArrayList集合为其指定泛型为Integer类型,
 7      * 并为其添加10个元素,利用迭代器遍历ArrayList集合,其循环条件为如果迭代器中仍有元素可以迭代,
 8      * 则继续循环,如果没有,则跳出循环。
 9      **/
10 
11     public static void main(String[] args) {
12         // TODO 自动生成的方法存根
13         //创建集合测试类
14         List<Integer> list=new ArrayList<Integer>();
15         //添加元素的范围
16         for(int i=0;i<10;i++)
17         {
18             list.add(i);//增加10个元素
19         }
20         //输入10个元素到集合中
21         System.out.println("输出列表中的全部元素");
22         //Iterator迭代
23         Iterator<Integer> it=list.iterator();
24         //如果迭代器中右下一个元素,继续循环
25         while(it.hasNext())
26         {
27             //将下一个元素输入到集合中,继续循环
28             System.out.print("\t"+it.next());
29         }
30 
31     }
32 
33 }

 

posted on 2016-05-29 15:51  曦~妍  阅读(212)  评论(0编辑  收藏  举报