foreach遍历扩展(二)
一、前言
假设存在一个数组,其遍历模式是根据索引进行遍历的;又假设存在一个HashTable,其遍历模式是根据键值进行遍历的;无论哪种集合,如果它们的遍历没有一个共同的接口,那么在客户端进行调用的时候,就需要对每种集合的具体类型进行它们各自的具体代码编写,当需求发生变化时,就必须修改我们的代码。并且客户端过多的关注集合内部的实现,代码的移植性就会变差,违反了开闭原则,这个时候迭代器就诞生了,现在我们来根据上一章 foreach遍历原理(一)实现我们自己的迭代器。
二、代码示例
class Program
{
static void Main(string[] args)
{
//使用接口IMyEnumerable代替MyList
IMyEnumerable list = new MyList();
//得到迭代器,在循环中针对迭代器编码,而不是集合MyList
IMyEnumerator enumerator = list.GetEnumerator();
while (enumerator.MoveNext())
{
object current = enumerator.Current;
Console.WriteLine(current);
}
Console.ReadKey();
}
/// <summary>
/// 要求所有的迭代器全部实现该接口
/// </summary>
interface IMyEnumerator
{
bool MoveNext();
object Current { get; }
}
/// <summary>
/// 要求所有的集合实现该接口
/// 这样一来,客户端就可以针对该接口编码,
/// 而无须关注具体的实现
/// </summary>
interface IMyEnumerable
{
IMyEnumerator GetEnumerator();
int Count { get; }
}
class MyList : IMyEnumerable
{
]{,,,,,,,,,};
IMyEnumerator myEnumerator;
public object this[int i]
{
get { return items[i]; }
set { this.items[i] = value; }
}
public int Count
{
get { return items.Length; }
}
public IMyEnumerator GetEnumerator()
{
if (myEnumerator == null)
{
myEnumerator = new MyEnumerator(this);
}
return myEnumerator;
}
}
class MyEnumerator : IMyEnumerator
{
;
MyList myList;
public MyEnumerator(MyList myList)
{
this.myList = myList;
}
public bool MoveNext()
{
> myList.Count)
{
index = ;
return false;
}
else
{
index++;
return true;
}
}
public object Current
{
]; }
}
}
}
运行结果:
三、疑问——为什么不把 IMyEnumerable 和 IMyEnumerator 接口写在同一个接口里面,例如新建一个接口名字为 IForeach,不使用迭代器,也能输出上面的结果
class Program
{
static void Main(string[] args)
{
IForeach iForeach = new MyList();
while (iForeach.MoveNext())
{
object current = iForeach.Current;
Console.WriteLine(current);
}
Console.ReadKey();
}
interface IForeach
{
bool MoveNext();
object Current { get; }
int Count { get; }
}
class MyList : IForeach
{
] { , , , , , , , , , };
;
public object this[int i]
{
get { return items[i]; }
set { this.items[i] = value; }
}
public int Count
{
get { return items.Length; }
}
public bool MoveNext()
{
> Count)
{
index = ;
return false;
}
else
{
index++;
return true;
}
}
public object Current
{
]; }
}
}
}
四、如果我现在有新需求要倒序输出,那么按照上面“三”的做法,就必须修改 MyList 类里面的代码,而且是大改。如果使用迭代器 ,我们只需要重新写类继承迭代器IMyEnumerator,替换一下迭代器,就能够实现MyList 的倒序输出。
新增一个倒序迭代器
class MyInvertEnumerator : IMyEnumerator
{
;
MyList myList;
public MyInvertEnumerator(MyList myList)
{
this.myList = myList;
index = myList.Count;
}
public bool MoveNext()
{
<)
{
index = myList.Count;
return false;
}
else
{
index--;
return true;
}
}
public object Current
{
get { return myList[index]; }
}
}
修改MyList集合里面获取迭代器的方法,然后运行就能够
class MyList : IMyEnumerable
{
]{,,,,,,,,,};
IMyEnumerator myEnumerator;
public object this[int i]
{
get { return items[i]; }
set { this.items[i] = value; }
}
public int Count
{
get { return items.Length; }
}
public IMyEnumerator GetEnumerator()
{
if (myEnumerator == null)
{
// myEnumerator = new MyEnumerator(this);//正序输出
myEnumerator = new MyInvertEnumerator(this);//倒序输出
}
return myEnumerator;
}
}
倒序输出完整代码:
class Program
{
static void Main(string[] args)
{
//使用接口IMyEnumerable代替MyList
IMyEnumerable list = new MyList();
//得到迭代器,在循环中针对迭代器编码,而不是集合MyList
IMyEnumerator enumerator = list.GetEnumerator();
while (enumerator.MoveNext())
{
object current = enumerator.Current;
Console.WriteLine(current);
}
Console.ReadKey();
}
/// <summary>
/// 要求所有的迭代器全部实现该接口
/// </summary>
interface IMyEnumerator
{
bool MoveNext();
object Current { get; }
}
/// <summary>
/// 要求所有的集合实现该接口
/// 这样一来,客户端就可以针对该接口编码,
/// 而无须关注具体的实现
/// </summary>
interface IMyEnumerable
{
IMyEnumerator GetEnumerator();
int Count { get; }
}
class MyList : IMyEnumerable
{
]{,,,,,,,,,};
IMyEnumerator myEnumerator;
public object this[int i]
{
get { return items[i]; }
set { this.items[i] = value; }
}
public int Count
{
get { return items.Length; }
}
public IMyEnumerator GetEnumerator()
{
if (myEnumerator == null)
{
// myEnumerator = new MyEnumerator(this);//正序输出
myEnumerator = new MyInvertEnumerator(this);//倒序输出
}
return myEnumerator;
}
}
class MyEnumerator : IMyEnumerator
{
;
MyList myList;
public MyEnumerator(MyList myList)
{
this.myList = myList;
}
public bool MoveNext()
{
> myList.Count)
{
index = ;
return false;
}
else
{
index++;
return true;
}
}
public object Current
{
]; }
}
}
class MyInvertEnumerator : IMyEnumerator
{
;
MyList myList;
public MyInvertEnumerator(MyList myList)
{
this.myList = myList;
index = myList.Count;
}
public bool MoveNext()
{
<)
{
index = myList.Count;
return false;
}
else
{
index--;
return true;
}
}
public object Current
{
get { return myList[index]; }
}
}
}
迭代器就讲到这里了,谢谢大家。
foreach遍历扩展(二)的更多相关文章
- c#--foreach遍历的用法与split的用法
一. foreach循环用于列举出集合中所有的元素,foreach语句中的表达式由关键字in隔开的两个项组成.in右边的项是集合名,in左边的项是变量名,用来存放该集合中的每个元素. 该循环 ...
- 用数组指针遍历数组,FOR/FOREACH遍历数组
1. 用数组指针遍历一维数组 <?php header("Content-type:text/html;charset=utf-8"); /*用数组指针遍历一位数组的值*/ ...
- foreach遍历数组
foreach遍历一维数组 <?php //PHP数组遍历:foreach //定义数组 $arr=array(1,2,3,4,5,6,7,8,9,10); //foreach循环 foreac ...
- C# 表达式树遍历(二)
一.前言 上一篇我们对表达式树有了初步的认识,这里我们将对表达式树进行遍历,只有弄清楚了他的运行原理,我们才可以对他进行定制化修改. 表达式系列目录 C# 表达式树讲解(一) C# 表达式树遍历(二) ...
- C#实现在foreach遍历中删除集合中的元素(方法总结)
目录 方法一:采用for循环,并且从尾到头遍历 方法二:使用递归 方法三:通过泛型类实现IEnumerator 在foreach中删除元素时,每一次删除都会导致集合的大小和元素索引值发生变化,从而导致 ...
- 关于for与forEach遍历集合中对集合进行操作的问题
遍历List集合,在循环中再对List集合进行操作,有时候会遇到ConcurrentModificationException(并发修改异常);其实只有在forEach循环集合再对集合操作会发生异常: ...
- 用<forEach>遍历list集合时,提示我找不到对象的属性
<c:forEach items="${list}" var="item"> <tr> <td>${item.UserId} ...
- Foreach遍历
前天在项目中遇到一个问题,foreach遍历过程中修改responses中的对象,其中responses的类型:IEnumerable<Order>,代码如下: foreach (Orde ...
- 使用yield关键字让自定义集合实现foreach遍历
一般来说当我们创建自定义集合的时候为了让其能支持foreach遍历,就只能让其实现IEnumerable接口(可能还要实现IEnumerator接口) 但是我们也可以通过使用yield关键字构建的迭代 ...
随机推荐
- jQuery的remove和detach的区别
1.remove([expr]) 概述:从DOM中删除所有匹配的元素. 这个方法不会把匹配的元素从jQuery对象中删除,因而可以在将来再使用这些匹配的元素.但除了这个元素本身得以保留之外,其他的 ...
- 自己寫的 Loading JS插件
本文為原創文章,轉載請注明出處,謝謝./** * @author samkin.yang * @version 1.0 */var $_yxj = new SamkinLoading(); (func ...
- 比file_get_contents稳定的curl_get_contents
相信使用过file_get_contents函数的朋友都知道,当获取的$url访问不了时,会导致页面漫长的等待,甚至还能导致PHP进程占用CPU达100%,因此这个函数就诞生了 分享一个实际在用的函数 ...
- JSON stringify and parse
来源 : http://javascript.ruanyifeng.com/stdlib/date.html //解析json也可以传入一个方法, 基本上和stringify差不多,不过是逆序的, 要 ...
- Sum square difference
简单: e sum of the squares of the first ten natural numbers is, 12 + 22 + ... + 102 = 385 The square o ...
- Android——在活动中使用Menu
ADT在HelloWorldActivity中自动创建了一个onCreateOptionsMenu()方法.这个方法是用于在活动中创建菜单的,现在我们来仔细分析一下了.手机毕竟和电脑不同,它的屏幕空间 ...
- 安卓应用使用QQ登录的申请流程
“QQ互联”是腾讯为第三方网站.媒体.终端提供的开放平台.QQ互联拥有8个组件,提供诸如分享.登陆.like.qq提醒等能力.开发者使用QQ帐号登陆组件可以降低了用户的注册门槛,减少注册环节的用户流失 ...
- 【笔试&面试】C#中的程序集
1. C#中的程序集(Assembly) 答:程序集是包含一个或多个类型定义文件和资源文件的集合.它允许我们分离可重用类型的逻辑表示和物理表示. 程序集是一个可重用.可实施版本策略和安全策略 ...
- COJ 0359 xjr考考你数据结构(根号2)线段树区间增加
xjr考考你数据结构(根号2) 难度级别:C: 运行时间限制:3000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 请你编写一个数据结构,完成以下功能: 1)求出第 ...
- BZOJ3314: [Usaco2013 Nov]Crowded Cows
3314: [Usaco2013 Nov]Crowded Cows Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 86 Solved: 61[Subm ...