当前位置: 首页 > news >正文 做网页难吗廊坊首位关键词优化电话 news 2025/10/23 12:01:04 做网页难吗,廊坊首位关键词优化电话,上海网站建设基础,网站建设小组的五类成员Go和Java实现迭代器模式 1、迭代器模式 迭代器模式是 Java 和 .Net 编程环境中非常常用的设计模式。这种模式用于顺序访问集合对象的元素#xff0c;不需要知道 集合对象的底层表示。 迭代器模式属于行为型模式。 意图#xff1a;提供一种方法顺序访问一个聚合对象中各个…Go和Java实现迭代器模式 1、迭代器模式 迭代器模式是 Java 和 .Net 编程环境中非常常用的设计模式。这种模式用于顺序访问集合对象的元素不需要知道 集合对象的底层表示。 迭代器模式属于行为型模式。 意图提供一种方法顺序访问一个聚合对象中各个元素, 而又无须暴露该对象的内部表示。 主要解决不同的方式来遍历整个整合对象。 何时使用遍历一个聚合对象。 如何解决把在元素之间游走的责任交给迭代器而不是聚合对象。 关键代码定义接口hasNext, next。 应用实例JAVA 中的 iterator。 优点1、它支持以不同的方式遍历一个聚合对象。 2、迭代器简化了聚合类。 3、在同一个聚合上可以有多 个遍历。 4、在迭代器模式中增加新的聚合类和迭代器类都很方便无须修改原有代码。 缺点由于迭代器模式将存储数据和遍历数据的职责分离增加新的聚合类需要对应增加新的迭代器类类的 个数成对增加这在一定程度上增加了系统的复杂性。 使用场景1、访问一个聚合对象的内容而无须暴露它的内部表示。 2、需要为聚合对象提供多种遍历方式。 3、为遍历不同的聚合结构提供一个统一的接口。 注意事项迭代器模式就是分离了集合对象的遍历行为抽象出一个迭代器类来负责这样既可以做到不暴露 集合的内部结构又可让外部代码透明地访问集合内部的数据。 适用性 访问一个聚合对象的内容而无需暴露它的内部表示。 支持对聚合对象的多种遍历。 为遍历不同的聚合结构提供一个统一的接口(即支持多态迭代)。 2、Go实现迭代器模式 package iterator// Iterator type Iterator interface {Next() stringFirst()Last()HasNext() bool }package iterator// IteratorImpl type IteratorImpl struct {List []stringIndex int }func (iteratorImpl *IteratorImpl) First() {iteratorImpl.Index 0 }func (iteratorImpl *IteratorImpl) Last() {iteratorImpl.Index len(iteratorImpl.List) - 1 }func (iteratorImpl *IteratorImpl) Next() string {obj : iteratorImpl.List[iteratorImpl.Index]iteratorImpl.Indexreturn obj }func (iteratorImpl *IteratorImpl) HasNext() bool {return iteratorImpl.Index len(iteratorImpl.List) }package iterator// List type List interface {Iterator() IteratorGet(index int) stringGetSize() intAdd(str string) }package iterator// ListImpl type ListImpl struct {Index intSize intList []string }func NewList() List {return ListImpl{Index: 0, Size: 0} }func (listImpl *ListImpl) Iterator() Iterator {return IteratorImpl{Index: 0, List: listImpl.List} }func (listImpl *ListImpl) Get(index int) string {return listImpl.List[index] }func (listImpl *ListImpl) GetSize() int {return len(listImpl.List) }func (listImpl *ListImpl) Add(str string) {listImpl.List append(listImpl.List, str) }package mainimport (fmt. proj/iterator )func main() {list : NewList()list.Add(a)list.Add(b)list.Add(c)// 第一种迭代方式iterator : list.Iterator()for iterator.HasNext() {fmt.Println(iterator.Next())}fmt.Println()// 第二种迭代方式for i : 0; i list.GetSize(); i {fmt.Println(list.Get(i))}fmt.Println()// first()和last()设置第一个元素和最后一个元素firstIt : list.Iterator()firstIt.First()for firstIt.HasNext() {fmt.Println(firstIt.Next())}fmt.Println()lastIt : list.Iterator()lastIt.Last()for lastIt.HasNext() {fmt.Println(lastIt.Next())} }# 程序输出 a b ca b ca b cc3、Java实现迭代器模式 package com.iterator;// Iterator public interface Iterator {Object next();void first();void last();boolean hasNext(); }package com.iterator;// IteratorImpl public class IteratorImpl implements Iterator {private List list;private int index;public IteratorImpl(List list){index 0;this.list list;}Overridepublic void first() {index 0;}Overridepublic void last() {index list.getSize() - 1;}Overridepublic Object next() {Object obj list.get(index);index;return obj;}Overridepublic boolean hasNext() {return index list.getSize();} }package com.iterator;// List public interface List {Iterator iterator();Object get(int index);int getSize();void add(Object obj); }package com.iterator;// ListImpl public class ListImpl implements List {private Object[] list;private int index;private int size;public ListImpl() {index 0;size 0;list new Object[100];}Overridepublic Iterator iterator() {return new IteratorImpl(this);}Overridepublic Object get(int index) {return list[index];}Overridepublic int getSize() {return this.size;}Overridepublic void add(Object obj) {list[index] obj;size;} }package com.iterator;public class Test {public static void main(String[] arg) {List list new ListImpl();list.add(a);list.add(b);list.add(c);// 第一种迭代方式Iterator it list.iterator();while (it.hasNext()){System.out.println(it.next());}System.out.println();// 第二种迭代方式for (int i 0; i list.getSize(); i) {System.out.println(list.get(i));}System.out.println();// first()和last()设置第一个元素和最后一个元素Iterator firstIt list.iterator();firstIt.first();while (firstIt.hasNext()){System.out.println(firstIt.next());}System.out.println();Iterator lastIt list.iterator();lastIt.last();while (lastIt.hasNext()){System.out.println(lastIt.next());}} }# 程序输出 a b ca b ca b cc 文章转载自: http://www.morning.jhxdj.cn.gov.cn.jhxdj.cn http://www.morning.jtrqn.cn.gov.cn.jtrqn.cn http://www.morning.wmfh.cn.gov.cn.wmfh.cn http://www.morning.cwknc.cn.gov.cn.cwknc.cn http://www.morning.snxbf.cn.gov.cn.snxbf.cn http://www.morning.fmkjx.cn.gov.cn.fmkjx.cn http://www.morning.hjbrd.cn.gov.cn.hjbrd.cn http://www.morning.ftmly.cn.gov.cn.ftmly.cn http://www.morning.ytmx.cn.gov.cn.ytmx.cn http://www.morning.ylmxs.cn.gov.cn.ylmxs.cn http://www.morning.qmqgx.cn.gov.cn.qmqgx.cn http://www.morning.nspbj.cn.gov.cn.nspbj.cn http://www.morning.dgmjm.cn.gov.cn.dgmjm.cn http://www.morning.fcqlt.cn.gov.cn.fcqlt.cn http://www.morning.ydwnc.cn.gov.cn.ydwnc.cn http://www.morning.qmncj.cn.gov.cn.qmncj.cn http://www.morning.txmlg.cn.gov.cn.txmlg.cn http://www.morning.gpfuxiu.cn.gov.cn.gpfuxiu.cn http://www.morning.wwkdh.cn.gov.cn.wwkdh.cn http://www.morning.dmzfz.cn.gov.cn.dmzfz.cn http://www.morning.smmrm.cn.gov.cn.smmrm.cn http://www.morning.ylqpp.cn.gov.cn.ylqpp.cn http://www.morning.chhhq.cn.gov.cn.chhhq.cn http://www.morning.ktntj.cn.gov.cn.ktntj.cn http://www.morning.ysckr.cn.gov.cn.ysckr.cn http://www.morning.ndltr.cn.gov.cn.ndltr.cn http://www.morning.jwqqd.cn.gov.cn.jwqqd.cn http://www.morning.bgdk.cn.gov.cn.bgdk.cn http://www.morning.kgmkl.cn.gov.cn.kgmkl.cn http://www.morning.smrty.cn.gov.cn.smrty.cn http://www.morning.xphcg.cn.gov.cn.xphcg.cn http://www.morning.qyqdz.cn.gov.cn.qyqdz.cn http://www.morning.qdlr.cn.gov.cn.qdlr.cn http://www.morning.dxqwm.cn.gov.cn.dxqwm.cn http://www.morning.gbfzy.cn.gov.cn.gbfzy.cn http://www.morning.frxsl.cn.gov.cn.frxsl.cn http://www.morning.qclmz.cn.gov.cn.qclmz.cn http://www.morning.rxwfg.cn.gov.cn.rxwfg.cn http://www.morning.jkzjs.cn.gov.cn.jkzjs.cn http://www.morning.yjfmj.cn.gov.cn.yjfmj.cn http://www.morning.kycxb.cn.gov.cn.kycxb.cn http://www.morning.khdw.cn.gov.cn.khdw.cn http://www.morning.bby45.cn.gov.cn.bby45.cn http://www.morning.webife.com.gov.cn.webife.com http://www.morning.qnzgr.cn.gov.cn.qnzgr.cn http://www.morning.zpdjh.cn.gov.cn.zpdjh.cn http://www.morning.xkqjw.cn.gov.cn.xkqjw.cn http://www.morning.tplht.cn.gov.cn.tplht.cn http://www.morning.kjksn.cn.gov.cn.kjksn.cn http://www.morning.jygsq.cn.gov.cn.jygsq.cn http://www.morning.lnrr.cn.gov.cn.lnrr.cn http://www.morning.nqbkb.cn.gov.cn.nqbkb.cn http://www.morning.xfmwk.cn.gov.cn.xfmwk.cn http://www.morning.sskhm.cn.gov.cn.sskhm.cn http://www.morning.xpgwz.cn.gov.cn.xpgwz.cn http://www.morning.ypcd.cn.gov.cn.ypcd.cn http://www.morning.rxhs.cn.gov.cn.rxhs.cn http://www.morning.hmgqy.cn.gov.cn.hmgqy.cn http://www.morning.qhvah.cn.gov.cn.qhvah.cn http://www.morning.kgqww.cn.gov.cn.kgqww.cn http://www.morning.prsxj.cn.gov.cn.prsxj.cn http://www.morning.hdnd.cn.gov.cn.hdnd.cn http://www.morning.qhvah.cn.gov.cn.qhvah.cn http://www.morning.gqnll.cn.gov.cn.gqnll.cn http://www.morning.tjpmf.cn.gov.cn.tjpmf.cn http://www.morning.cwyfs.cn.gov.cn.cwyfs.cn http://www.morning.nfgbf.cn.gov.cn.nfgbf.cn http://www.morning.tknqr.cn.gov.cn.tknqr.cn http://www.morning.drmbh.cn.gov.cn.drmbh.cn http://www.morning.xphls.cn.gov.cn.xphls.cn http://www.morning.tgfsr.cn.gov.cn.tgfsr.cn http://www.morning.dmhs.cn.gov.cn.dmhs.cn http://www.morning.gpsr.cn.gov.cn.gpsr.cn http://www.morning.gqtzb.cn.gov.cn.gqtzb.cn http://www.morning.jfbpf.cn.gov.cn.jfbpf.cn http://www.morning.bqpg.cn.gov.cn.bqpg.cn http://www.morning.skqfx.cn.gov.cn.skqfx.cn http://www.morning.zdhnm.cn.gov.cn.zdhnm.cn http://www.morning.dsxgc.cn.gov.cn.dsxgc.cn http://www.morning.hlyfn.cn.gov.cn.hlyfn.cn 查看全文 http://www.tj-hxxt.cn/news/242443.html 相关文章: 做招聘网站需要什么人员金华市有网站建设最低价 如何设定旅游网站seo核心关键词ps最好用的素材网站 php模板网站在线可以做翻译的网站吗 中山网站制作网页电商网站建设会计分录 免费制作网站方案室内设计网站免费素材 新闻营销发稿平台百度广告优化 烟台网站建设方案书咕叽网 wordpress 北京网站怎么建设购物网站 英文介绍 做高端品牌生产商的网站南山做网站的 山西怀仁建设银行佛山网站优化公司 php 免费装修网站注册一个商标多少钱 网站管理有哪些扬中新网网 个人网站申请空间企业seo排名 网站建设公司长春专业设计网站有哪些 微信商城网站案例展示网站有标题 利于优化的网站装潢设计师培训 河南网站排名优化免费网页空间到哪申请 电子商务网站建设调查分析wordpress怎么打删除线 做购物网站需要多少钱wordpress 主题破解版 模板网站难做seodz仿网站头部 泸州网站建设唐网互联邢台房产信息网58同城 如何在自己网站做解析api西安看个号网络科技有限公司 萧山建站网络营销推广公司哪家好 课程的网站建设黄石网站建设价格 关于建设教体局网站的申请公众号官方 搜狐三季度营收多少网站关键词优化多少钱 网站解析不过来wordpress建站教程百科 对于网站建设的提问用php做网站的书籍 精品课程网站源码网站 手机网站 腾讯理财是什么样的做网站php做网站子页模板