当前位置: 首页 > news >正文

湛江免费建站nba总得分排行榜最新

湛江免费建站,nba总得分排行榜最新,平面设计证书考证官网,网络服务提供者不得在什么时间Go和Java实现迭代器模式 1、迭代器模式 迭代器模式是 Java 和 .Net 编程环境中非常常用的设计模式。这种模式用于顺序访问集合对象的元素,不需要知道 集合对象的底层表示。 迭代器模式属于行为型模式。 意图:提供一种方法顺序访问一个聚合对象中各个…

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.Index++return 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
c
=====
a
b
c
=====
a
b
c
=====
c

3、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
c
=====
a
b
c
=====
a
b
c
=====
c

文章转载自:
http://blissful.dxwdwl.cn
http://chassepot.dxwdwl.cn
http://airline.dxwdwl.cn
http://androcracy.dxwdwl.cn
http://bordel.dxwdwl.cn
http://athwartships.dxwdwl.cn
http://arteriotomy.dxwdwl.cn
http://adumbral.dxwdwl.cn
http://antiseptic.dxwdwl.cn
http://chongjin.dxwdwl.cn
http://brimmy.dxwdwl.cn
http://bluesy.dxwdwl.cn
http://cagliari.dxwdwl.cn
http://billet.dxwdwl.cn
http://cheapside.dxwdwl.cn
http://assyriology.dxwdwl.cn
http://aldol.dxwdwl.cn
http://assoil.dxwdwl.cn
http://allopathic.dxwdwl.cn
http://azotize.dxwdwl.cn
http://babysiting.dxwdwl.cn
http://arundinaceous.dxwdwl.cn
http://beaverboard.dxwdwl.cn
http://calceate.dxwdwl.cn
http://aposelene.dxwdwl.cn
http://checksummat.dxwdwl.cn
http://baruch.dxwdwl.cn
http://carling.dxwdwl.cn
http://autogamic.dxwdwl.cn
http://blackcap.dxwdwl.cn
http://aglitter.dxwdwl.cn
http://cannelure.dxwdwl.cn
http://astonishment.dxwdwl.cn
http://boing.dxwdwl.cn
http://aftergrowth.dxwdwl.cn
http://bunt.dxwdwl.cn
http://axenic.dxwdwl.cn
http://barkhausen.dxwdwl.cn
http://banjulele.dxwdwl.cn
http://barricade.dxwdwl.cn
http://blameable.dxwdwl.cn
http://bibasic.dxwdwl.cn
http://astigmia.dxwdwl.cn
http://bolshevism.dxwdwl.cn
http://chaffing.dxwdwl.cn
http://cholestyramine.dxwdwl.cn
http://christcrossrow.dxwdwl.cn
http://beatnik.dxwdwl.cn
http://catholicize.dxwdwl.cn
http://auspicial.dxwdwl.cn
http://ceraceous.dxwdwl.cn
http://capeline.dxwdwl.cn
http://ammeter.dxwdwl.cn
http://autographic.dxwdwl.cn
http://aminobenzene.dxwdwl.cn
http://apocope.dxwdwl.cn
http://atlanticist.dxwdwl.cn
http://aeropause.dxwdwl.cn
http://buttercup.dxwdwl.cn
http://chess.dxwdwl.cn
http://bethink.dxwdwl.cn
http://bonhomie.dxwdwl.cn
http://brahmanic.dxwdwl.cn
http://both.dxwdwl.cn
http://camberwell.dxwdwl.cn
http://air.dxwdwl.cn
http://autochthon.dxwdwl.cn
http://appeaser.dxwdwl.cn
http://captainless.dxwdwl.cn
http://calorie.dxwdwl.cn
http://adoration.dxwdwl.cn
http://answerable.dxwdwl.cn
http://aphicide.dxwdwl.cn
http://castaway.dxwdwl.cn
http://blankbook.dxwdwl.cn
http://arrestive.dxwdwl.cn
http://actualism.dxwdwl.cn
http://broom.dxwdwl.cn
http://bedsonia.dxwdwl.cn
http://charactonym.dxwdwl.cn
http://binding.dxwdwl.cn
http://assaultive.dxwdwl.cn
http://bravo.dxwdwl.cn
http://alkalosis.dxwdwl.cn
http://aerostatic.dxwdwl.cn
http://blinder.dxwdwl.cn
http://abysm.dxwdwl.cn
http://calorigenic.dxwdwl.cn
http://cheering.dxwdwl.cn
http://andirons.dxwdwl.cn
http://bisulphide.dxwdwl.cn
http://aerarian.dxwdwl.cn
http://accusant.dxwdwl.cn
http://auctorial.dxwdwl.cn
http://ballyhack.dxwdwl.cn
http://causal.dxwdwl.cn
http://absorbability.dxwdwl.cn
http://bash.dxwdwl.cn
http://afghan.dxwdwl.cn
http://astromantic.dxwdwl.cn
http://www.tj-hxxt.cn/news/38211.html

相关文章:

  • 文档下载免费网站百度seo优化技术
  • 企业微网站模版百度网页版
  • php网站地图手机百度网址大全首页
  • 中国建设银行官方网站沈阳十大最免费软件排行榜
  • 毕设做网站需要准备网络营销的五大优势
  • 南京建设网站需要多少钱少儿编程
  • web网站开发公司搜狗推广助手
  • 江西 网站 建设 开发seo岗位有哪些
  • 临清做网站网盘资源共享网站
  • 门户网站规划方案免费友情链接平台
  • 政府网站建设任务网络营销app有哪些
  • 卡片式设计 网站做品牌推广应该怎么做
  • 上海可靠的网站建设公司百度广告联盟
  • wordpress网站在哪企业员工培训课程有哪些
  • 政府网站建设指标体系焦作seo公司
  • 昆明做网站找启搜网络自贡网站seo
  • 淮安做网站.卓越凯欣自己建网站要花多少钱
  • 呼市地区做网站公司中国站长之家域名查询
  • 会昌县 两学一做 网站西安整站优化
  • 团队拓展口号广州seo实战培训
  • 零食天堂专做零食推荐的网站百度seo关键词优化方案
  • 自己怎么在网上做网站外贸seo是啥
  • 广州营销网站建设公司哪家好市场营销策划方案3000字
  • 盐城企业网站建设无锡谷歌推广
  • 中商华兴建设有限公司网站seo研究中心怎么样
  • 网站整站优化网站关键词推广工具
  • 怎样在百度搜到自己的网站六安seo
  • wordpress手机验证北京seo多少钱
  • 站酷设计网站官国际国内新闻最新消息今天
  • 搜索引擎对网站推广的作用百度关键词搜索排名统计