• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
火磷
Memory will fade,but not notes.
博客园    首页    新随笔    联系   管理    订阅  订阅
Iterator接口

1.定义

迭代器是一种设计模式,它是一个对象,可以遍历并选择序列中的对象。较多用于ArrayList,LinkedList。

2.方法

1 public interface Iterator {
2 
3              boolean hasNext();               //检查序列中是否还有元素
4              Object next();                       //获得序列中的下一个元素
5              void remove();                      //移除序列中的元素
6 }

3.实例

创建一个可以实现遍历操作的方法,然后使用对象调用该方法(方便对多个对象进行遍历)

 1 public void iteratecollection(Collection c){
 2         Iterator it=c.iterator();
 3         while(it.hasNext()){
 4             Object obj=it.next();
 5             System.out.println(obj);
 6         }
 7     }
 8 
 9 List l1=new ArrayList();
10         l1.add("AA");
11         l1.add("CC");
12         l1.add("BB");
13         l1.add("DD");
14      iteratecollection(l1);  





 

posted on 2015-08-07 11:42  火磷  阅读(217)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3