(行为型模式八)迭代器模式
来源:互联网 发布:java 命令打包成jar包 编辑:程序博客网 时间:2024/06/05 21:13
package com.eyugame.modle;import java.util.ArrayList;import java.util.List;/** * 迭代器模式 * @author JYC506 * * @param <T> *//*自定义迭代器*/public class MyIterator<T> implements Iterator<T> {private List<T> list = new ArrayList<T>();private int cursor;public MyIterator(List<T> list) {super();this.list = list;this.cursor=0;}@Overridepublic boolean hasNext() {if (this.cursor < list.size()) {return true;}return false;}@Overridepublic T next() {T t = list.get(cursor);cursor++;return t;}public static void main(String[] args) {MyList<User> list=new MyList<User>();list.add(new User("小明",9));list.add(new User("小红",7));list.add(new User("小丽",8));Iterator<User> iterator=list.iterator();while(iterator.hasNext()){User user=iterator.next();System.out.println("姓名:"+user.getUsername()+" 年龄"+user.getAge());}}}interface Iterator<T> {boolean hasNext();T next();}/*自定义列表*/class MyList<T>{List<T> list=new ArrayList<T>();public void add(T t){list.add(t);}public void remove(T t){list.remove(t);}public Iterator<T> iterator(){return new MyIterator<>(list);}}class User {private String username;private int age;public User(String username, int age) {super();this.username = username;this.age = age;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}
0 0
- (行为型模式八)迭代器模式
- 八、状态模式State(行为型模式)
- 行为型模式-----迭代器模式(Iterator)
- 迭代器模式(行为型)
- 行为型模式--迭代器模式
- 行为型模式-迭代器模式
- 设计模式--行为型模式之八--State
- 行为型模式之八——状态模式
- 行为模式之八---State
- 行为型--迭代器模式
- 迭代器模式(行为型)
- 设计模式(迭代器模式)----- 行为型模式
- Java设计模式之行为型模式(迭代器模式)
- 设计模式 - 行为型模式 - 迭代器模式
- 设计模式->行为型模式->迭代器模式
- 设计模式(八)the Template method 模板方法模式(类行为型)
- 行为模式-迭代器模式
- 行为模式-迭代器模式
- NDK-JNI实战教程(二) JNI官方中文资料
- 大波妹SEX外送茶+line:sweet558台北叫小姐台中找茶/北外送服務板橋找女人
- QML之动态菜单配置
- 用eclipse向github上传代码
- 我的linux学习
- (行为型模式八)迭代器模式
- 关于VTK+QT在关闭程序后进程无法结束的问题
- struts2自定义类型转换器
- stm8编译器的安装,及一些问题;
- Installation error: INSTALL_FAILED_VERSION_DOWNGRADE
- 输入一个字符串,字符串中含有字母、逗号、空格,实现将输入字符中的逗号,空格号去除,字母分行显示
- 《深入剖析Tomcat 》第3章 连接器(Connector)
- C++经典问题解析
- 一种多线程安全的单例模式