第一步:没有接口的迭代器简单实现原理

 

 1 package com.bjsxt.xiaofei;
 2 /**
 3  * 迭代器底层原理
 4  * 方法:
 5  * hasNext()
 6  * next()
 7  * remove()
 8 * @ClassName: MyAarryList 
 9 * @Description: TODO(这里用一句话描述这个类的作用) 
10 * @author 尚晓飞
11 * @date 2014-7-29 下午7:06:09 
12 *
13  */
14 public class MyAarryList {
15     //容器底层是数组
16     private String[] str={"a","b","c","d","e","f","g","h"};
17     //数组的长度
18     private int size=str.length;
19     //游标
20     private int cursor=-1;
21     
22     /**
23      * 判断迭代器是否有下一个元素
24     * @Title: hasNext 
25     * @Description: TODO(这里用一句话描述这个方法的作用) 
26     * @return
27     * @return boolean    返回类型 
28     * @author 尚晓飞
29     * @date 2014-7-29 下午7:09:50
30      */
31     public boolean hasNext(){
32         return cursor+1<size;
33     }
34     
35     /**
36      * 获取下一个元素
37     * @Title: next 
38     * @Description: TODO(这里用一句话描述这个方法的作用) 
39     * @return
40     * @return String    返回类型 
41     * @author 尚晓飞
42     * @date 2014-7-29 下午7:10:36
43      */
44     public String next(){
45         cursor++;
46         return str[cursor];
47     }
48     
49     /**
50      * 移除
51     * @Title: remove 
52     * @Description: TODO(这里用一句话描述这个方法的作用) 
53     * @return void    返回类型 
54     * @author 尚晓飞
55     * @date 2014-7-29 下午7:20:39
56      */
57     public void remove(){
58         //没有实现
59     }
60     
61     public static void main(String[] args) {
62         MyAarryList list=new MyAarryList();
63         //测试简易迭代器
64         while (list.hasNext()) {
65             String element=list.next();
66             System.out.println("MyAarryList.main()"+element);
67             
68         }
69     }
70 }
View Code

相关文章:

  • 2022-12-23
  • 2021-07-10
  • 2021-11-17
  • 2022-01-25
  • 2021-10-30
  • 2021-09-24
猜你喜欢
  • 2021-12-06
  • 2021-09-25
  • 2021-06-28
  • 2021-11-10
  • 2021-05-09
  • 2021-11-24
相关资源
相似解决方案