自写一个模仿Dictionary与Foreach的实现及心得总结

所属分类: 网络编程 / ASP.NET 阅读数: 875
收藏 0 赞 0 分享
自己写一个类模仿Dictionary实现
a、自定义字典类MyDic
复制代码 代码如下:

using System.Collections.Generic;
namespace _10_自己写Dictionary {
class KeyValuePair {
public KeyValuePair() {
}
public KeyValuePair(string key, string value) {
this.key = key;
this.value = value;
}
private string key;
public string Key {
get {
return key;
}
set {
key = value;
}
}
private string value;
public string Value {
get {
return this .value;
}
set {
this.value = value ;
}
}
}
class MyDic {
List<KeyValuePair > list = new List<KeyValuePair >();
public void Add(string key, string value) {
list.Add( new KeyValuePair (key, value));
}
public bool ContainsKey(string key) {
bool res = false ;
foreach(KeyValuePair item in list) {
if(item.Key == key) {
res = true;
break;
}
}
return res;
}
}
}

b、调用测试
复制代码 代码如下:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
namespace _10_自己写Dictionary {
class Program {
static void Main(string[] args) {
//Dictionary方法实现
Dictionary<string , string> dic = new Dictionary <string, string>();
string[] filecon = File .ReadAllLines("英汉词典TXT格式.txt", Encoding.Default);
for(int i = 0; i < filecon.Count(); i++) {
string[] arr = filecon[i].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
if(!dic.ContainsKey(arr[0])) {
dic.Add(arr[0], arr[1]);
}
}
Stopwatch sw = new Stopwatch();
sw.Start();
dic.ContainsKey( "china");
sw.Stop();
Console.WriteLine(sw.Elapsed);//00:00:00:0000055;
//自己写的list实现
MyDic mydic = new MyDic();
string[] filecon2 = File .ReadAllLines("英汉词典TXT格式.txt", Encoding.Default);
for(int i = 0; i < filecon2.Count(); i++) {
string[] arr = filecon2[i].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
if(!mydic.ContainsKey(arr[0])) {
mydic.Add(arr[0], arr[1]);
}
}
Stopwatch sw2 = new Stopwatch();
sw2.Start();
mydic.ContainsKey( "china");
sw2.Stop();
Console.WriteLine(sw2.Elapsed);//00:00:00:0001287;慢了多少倍!!! 因为dictionary比list多了字典目录
Console.Read();
}
}
}

b中测试结果显示自己模仿的没有.Net FrameWork提供的快 为什么呢?

答:Dictionary中有一个存储键值对的区域,这个区域的每个存储单元有地址编号,根据hashCode算法,计算key的值的键值对应该存储的地址,将键值对放入指定的地址即可。查找的时候首先计算key的地址,就可以找到数据了。根据key找房间号,而不是逐个房间找。(*)或者说:当把一个kvp,采用一个固定算法(散列算法)根据key来计算这个kvp存放的地址。取的时候也是根据要找的key可以快速算出kvp存放的地址。

面试题中经常会问Foreach实现了什么接口这个问题很好回答,那我们能不能自己模仿实现Foreach呢?
c、Foreach内部原理:IEnumerable接口 自己实现IEnumerable
复制代码 代码如下:

using System.Collections;//引入IEnumerable所在命名空间
namespace IEnumerater {
class MyList : IEnumerable {//实现接口IEnumerable 它就一个IEnumerator声明枚举器的方法
ArrayList ary = new ArrayList();
public void Add(string name) {
ary.Add(name);
}
//自己写索引器 形式类似属性 作用类似枚举 方便快捷的方式 访问集合中的元素
public string this[ int index] {//int类型
get {
return ary[index].ToString();
} //index>ary.Count时超出索引界限
//set { }
}
public int this[ string name] {//string类型 通过name查找索引 参数类型自己决定 返回类型自己决定
get {
for(int i = 0; i < ary.Count; i++) {
if(ary[i] == name) {
return i;
}
}
return -1;
}
}
public IEnumerator GetEnumerator() {//IEnumerator F12跳转定义这里可以发现foreach只允许读取数据,而不能修改数据
for(int i = 0; i < ary.Count; i++) {
yield return ary[i].ToString();// yield关键字 可以看到 实现IEnumerator(枚举器)接口中MoveNext(指向下一条)方法 和Current(获取当前元素 因为只有get 所以可以理解为什么foreach不能修改值的原因了) 以及Reset重置索引
}
}
}
}

d、调用自己的IEnumerable
复制代码 代码如下:

using System;
namespace IEnumerater {
class Program {
static void Main(string[] args) {
//自己写一个类 实现了IEnumerable接口的getEnumerator()方法 就可以实现foreach的操作
MyList mylist = new MyList();
mylist.Add( "wanghao");//调用自己的add(string)方法
mylist.Add( "nihao");
mylist.Add( "buhao");
Console.WriteLine(mylist[1]);//使用自己的索引
Console.WriteLine(mylist["nihao" ].ToString());
foreach(string item in mylist) {
Console.WriteLine(item);
//item = "hello"; 不能使用foreach改变值
}
Console.Read();
}
}
}

总结
如果一个类进行foreach的话,该类必须实现IEnumerable,集合要支持foreach方式的遍历,必须实现IEnumerable接口(还要以某种方式返回实现了IEnumerator 的对象)
更多精彩内容其他人还在看

ASP.NET 水晶报表打印功能实现代码

ASP.NET下的水晶报表打印,据我所知有以下几种办法可以打印
收藏 0 赞 0 分享

ASP.Net 图片存入数据库的实现代码

在很多时候,我们有这样的需求:把图片存入到数据库当中。在一些应用程序中,我们可能有一些敏感的资料,由于存储在文件系统(file system)中的东西,将很容易被某些用户盗取,所以这些数据不能存放在文件系统中。
收藏 0 赞 0 分享

让Silverlight 2.0动画动起来Making Silverlight 2.0 animation Start(不能运动原因)

Microsoft Expression Blend 2 制作动画个人感觉倒像3DMAX 可以自动捕捉关键帧
收藏 0 赞 0 分享

asp.net Reporting Service在Web Application中的应用

由于我们这个项目中使用微软的报表服务(Reporting Services)作为报表输出工具,本人也对它进行一点点研究,虽没有入木三分,但这点知识至少可以在大部分Reporting Service的场景中应用。
收藏 0 赞 0 分享

C# 文件上传 默认最大为4M的解决方法

.net中默只能上传小于4m的文件,大于4M将无法显示页面.那么如何设置来使imputfile能上传更大的文件呢
收藏 0 赞 0 分享

asp.net 购物车实现详细代码

asp.net 购物车实现详细代码
收藏 0 赞 0 分享

asp.net repeater实现批量删除时注册多选框id到客户端

repeater批量删除时注册多选框id到客户端的实现代码
收藏 0 赞 0 分享

asp.net aspnetpager分页统计时与实际不符的解决办法

最近分页方面根据实际需要修改了一些函数
收藏 0 赞 0 分享

iis 服务器应用程序不可用的解决方法

访问页面时提示 服务器应用程序不可用,大家可以按照下面的方法重新注册下,应该能好点
收藏 0 赞 0 分享

asp.net button 绑定多个参数

asp.net button 绑定多个参数的代码
收藏 0 赞 0 分享
查看更多