Java: Iterator/Cursor Patterns
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | /** * 版权所有 2022 涂聚文有限公司 * 许可信息查看: * 描述: * 迭代器模式 Iterator/Cursor Patterns * 历史版本: JDK 14.02 * 2022-09-12 创建者 geovindu * 2022-09-12 添加 Lambda * 2022-09-12 修改:date * 接口类 * 2022-09-12 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc Vecterator.java * Interface * Record * Annotation * Enum * */ package com.javapatterns.iterator; import java.util.Iterator; import java.util.Vector; import java.util.NoSuchElementException; import java.lang.UnsupportedOperationException; /** * * * */ public class Vecterator implements Iterator{ /** * * * */ Vector iteratee; /** * * * */ int count; /** * * * */ public Vecterator(Vector v) { iteratee = v; count = 0 ; } /** * * * */ public boolean hasNext() { return count < iteratee.size(); } /** * * * */ public Object next() { synchronized (iteratee) { if (count < iteratee.size()) { return iteratee.elementAt(count++); } } throw new NoSuchElementException( "Vector Iterator" ); } /** * * * */ public void remove() { throw new UnsupportedOperationException(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | /** * 版权所有 2022 涂聚文有限公司 * 许可信息查看: * 描述: * 迭代器模式 Iterator/Cursor Patterns * 历史版本: JDK 14.02 * 2022-09-12 创建者 geovindu * 2022-09-12 添加 Lambda * 2022-09-12 修改:date * 接口类 * 2022-09-12 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc Desciple.java * Interface * Record * Annotation * Enum * */ package com.javapatterns.iterator; /** *抽象 门徒 * * */ abstract public class Desciple { /** *讲话 * * */ abstract public void speak(); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | /** * 版权所有 2022 涂聚文有限公司 * 许可信息查看: * 描述: * 迭代器模式 Iterator/Cursor Patterns * 历史版本: JDK 14.02 * 2022-09-12 创建者 geovindu * 2022-09-12 添加 Lambda * 2022-09-12 修改:date * 接口类 * 2022-09-12 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc Horse.java * Interface * Record * Annotation * Enum * */ package com.javapatterns.iterator; /** *白马 小白龙 * * */ public class Horse extends Desciple{ /** * * * */ public void speak() { System.out.println( "我是小白龙!" ); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | /** * 版权所有 2022 涂聚文有限公司 * 许可信息查看: * 描述: * 迭代器模式 Iterator/Cursor Patterns * 历史版本: JDK 14.02 * 2022-09-12 创建者 geovindu * 2022-09-12 添加 Lambda * 2022-09-12 修改:date * 接口类 * 2022-09-12 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc Monkey.java * Interface * Record * Annotation * Enum * */ package com.javapatterns.iterator; /** *孙大圣美猴王 * * */ public class Monkey extends Desciple{ /** * * * */ public void speak() { System.out.println( "我是孙猴子" ); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | /** * 版权所有 2022 涂聚文有限公司 * 许可信息查看: * 描述: * 迭代器模式 Iterator/Cursor Patterns * 历史版本: JDK 14.02 * 2022-09-12 创建者 geovindu * 2022-09-12 添加 Lambda * 2022-09-12 修改:date * 接口类 * 2022-09-12 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc Pigsy.java * Interface * Record * Annotation * Enum * */ package com.javapatterns.iterator; /** *猪八戒 * * */ public class Pigsy extends Desciple{ /** * * * */ public void speak() { System.out.println( "我是猪八戒" ); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | /** * 版权所有 2022 涂聚文有限公司 * 许可信息查看: * 描述: * 迭代器模式 Iterator/Cursor Patterns * 历史版本: JDK 14.02 * 2022-09-12 创建者 geovindu * 2022-09-12 添加 Lambda * 2022-09-12 修改:date * 接口类 * 2022-09-12 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc Sandy.java * Interface * Record * Annotation * Enum * */ package com.javapatterns.iterator; /** *沙僧沙和尚 * * */ public class Sandy extends Desciple{ /** * * * */ public void speak() { System.out.println( "我是沙和尚" ); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | /** * 版权所有 2022 涂聚文有限公司 * 许可信息查看: * 描述: * 迭代器模式 Iterator/Cursor Patterns * 历史版本: JDK 14.02 * 2022-09-12 创建者 geovindu * 2022-09-12 添加 Lambda * 2022-09-12 修改:date * 接口类 * 2022-09-12 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc MonkTang.java * Interface * Record * Annotation * Enum * */ package com.javapatterns.iterator; import java.util.Vector; import java.util.Iterator; import java.util.ListIterator; /** *唐僧 * * */ public class MonkTang { /** * * */ private static Vector desciples; /** * * * */ private Desciple lnkDesciple; /** * * * */ public MonkTang() { } /** * * * */ public void Show() { desciples = new Vector( 3 ); Desciple monkey = new Monkey(); Desciple pigsy = new Pigsy(); Desciple sandy = new Sandy(); desciples.add(monkey); desciples.add(pigsy); desciples.add(sandy); System.out.println( "The following is from polymorphic iterator(多态迭代器)" ); listPolymorphic(); System.out.println( "The following is from concrete iterator(具体迭代器)" ); listConcrete(); System.out.println( "The above are also external iterators(外禀迭代器)" ); System.out.println( "The follwoing is from internal iterator(内禀迭代器)" ); listInternal(); System.out.println( "The follwoing from robust iterator(健壮迭代器)" ); listRobust(); } /** * * * */ private static void listPolymorphic() { Desciple desciple ; Iterator it = desciples.iterator(); while (it.hasNext()) { desciple = (Desciple) it.next(); desciple.speak(); } } /** * * * */ private static void listConcrete() { Desciple desciple ; Vecterator vect = new Vecterator(desciples); while (vect.hasNext()) { desciple = (Desciple) vect.next(); desciple.speak(); } } /** * * * */ private static void listInternal() { Desciple desciple ; for ( int i = 0 ; i < desciples.size() ; i++) { desciple = (Desciple) desciples.elementAt(i); desciple.speak(); } } /** * * * */ private static void listRobust() { Desciple desciple ; ListIterator it = desciples.listIterator(); while (it.hasNext()) { desciple = (Desciple) it.next(); if (desciple instanceof Monkey ) { it.remove(); it.next(); it.add( new Horse()); it.previous(); } desciple.speak(); } } } |
调用:
1 2 3 | //迭代器模式 MonkTang monkTang= new MonkTang(); monkTang.Show(); |
输出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | The following is from polymorphic iterator(多态迭代器) 我是孙猴子 我是猪八戒 我是沙和尚 The following is from concrete iterator(具体迭代器) 我是孙猴子 我是猪八戒 我是沙和尚 The above are also external iterators(外禀迭代器) The follwoing is from internal iterator(内禀迭代器) 我是孙猴子 我是猪八戒 我是沙和尚 The follwoing from robust iterator(健壮迭代器) 我是孙猴子 我是小白龙! 我是沙和尚 |
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
分类:
Java
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
2011-09-25 SQLHelper.cs