您当前的位置: 首页 > 

命运之手

暂无认证

  • 1浏览

    0关注

    747博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

【设计模式】【16】迭代器模式

命运之手 发布时间:2022-04-24 10:39:03 ,浏览量:1

应用场景

迭代器模式,英文名Iterator Pattern

该模式专门提供一个类,来访问和管理容器中的对象

该模式优点是可以屏蔽容器的实现细节,用户只需关心迭代功能本身,对于复杂的容器类来说,尤其实用

实现代码


	@SuppressWarnings("all")
	public class Iterator {
	
	    List container = new ArrayList();
	
	    int cursor = 0;
	
	    public Iterator(List container) {
	        this.container = container;
	    }
	
	    public boolean hasNext() {
	        if (this.cursor == this.container.size())
	            return false;
	        else
	            return true;
	    }
	
	    public Object next() {
	        if (hasNext())
	            return container.get(cursor++);
	        else
	            return null;
	    }
	
	    public void remove() {
	        container.remove(cursor);
	    }
	}

关注
打赏
1654938663
查看更多评论
立即登录/注册

微信扫码登录

0.0365s