自己寫一個(gè)類模仿Dictionary實(shí)現(xiàn)
a、自定義字典類MyDic
復(fù)制代碼 代碼如下:
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 {
ListKeyValuePair > list = new ListKeyValuePair >();
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、調(diào)用測(cè)試
復(fù)制代碼 代碼如下:
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方法實(shí)現(xiàn)
Dictionarystring , 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實(shí)現(xiàn)
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;慢了多少倍!??! 因?yàn)閐ictionary比list多了字典目錄
Console.Read();
}
}
}
b中測(cè)試結(jié)果顯示自己模仿的沒有.Net FrameWork提供的快 為什么呢?
答:Dictionary中有一個(gè)存儲(chǔ)鍵值對(duì)的區(qū)域,這個(gè)區(qū)域的每個(gè)存儲(chǔ)單元有地址編號(hào),根據(jù)hashCode算法,計(jì)算key的值的鍵值對(duì)應(yīng)該存儲(chǔ)的地址,將鍵值對(duì)放入指定的地址即可。查找的時(shí)候首先計(jì)算key的地址,就可以找到數(shù)據(jù)了。根據(jù)key找房間號(hào),而不是逐個(gè)房間找。(*)或者說:當(dāng)把一個(gè)kvp,采用一個(gè)固定算法(散列算法)根據(jù)key來計(jì)算這個(gè)kvp存放的地址。取的時(shí)候也是根據(jù)要找的key可以快速算出kvp存放的地址。
面試題中經(jīng)常會(huì)問Foreach實(shí)現(xiàn)了什么接口這個(gè)問題很好回答,那我們能不能自己模仿實(shí)現(xiàn)Foreach呢?
c、Foreach內(nèi)部原理:IEnumerable接口 自己實(shí)現(xiàn)IEnumerable
復(fù)制代碼 代碼如下:
using System.Collections;//引入IEnumerable所在命名空間
namespace IEnumerater {
class MyList : IEnumerable {//實(shí)現(xiàn)接口IEnumerable 它就一個(gè)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時(shí)超出索引界限
//set { }
}
public int this[ string name] {//string類型 通過name查找索引 參數(shù)類型自己決定 返回類型自己決定
get {
for(int i = 0; i ary.Count; i++) {
if(ary[i] == name) {
return i;
}
}
return -1;
}
}
public IEnumerator GetEnumerator() {//IEnumerator F12跳轉(zhuǎn)定義這里可以發(fā)現(xiàn)foreach只允許讀取數(shù)據(jù),而不能修改數(shù)據(jù)
for(int i = 0; i ary.Count; i++) {
yield return ary[i].ToString();// yield關(guān)鍵字 可以看到 實(shí)現(xiàn)IEnumerator(枚舉器)接口中MoveNext(指向下一條)方法 和Current(獲取當(dāng)前元素 因?yàn)橹挥術(shù)et 所以可以理解為什么foreach不能修改值的原因了) 以及Reset重置索引
}
}
}
}
d、調(diào)用自己的IEnumerable
復(fù)制代碼 代碼如下:
using System;
namespace IEnumerater {
class Program {
static void Main(string[] args) {
//自己寫一個(gè)類 實(shí)現(xiàn)了IEnumerable接口的getEnumerator()方法 就可以實(shí)現(xiàn)foreach的操作
MyList mylist = new MyList();
mylist.Add( "wanghao");//調(diào)用自己的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();
}
}
}
總結(jié):
如果一個(gè)類進(jìn)行foreach的話,該類必須實(shí)現(xiàn)IEnumerable,集合要支持foreach方式的遍歷,必須實(shí)現(xiàn)IEnumerable接口(還要以某種方式返回實(shí)現(xiàn)了IEnumerator 的對(duì)象)
您可能感興趣的文章:- Repeater綁定dictionary數(shù)據(jù)源代碼及報(bào)錯(cuò)解決
- C# Dictionary的使用實(shí)例代碼
- C#中Dictionary幾種遍歷的實(shí)現(xiàn)代碼
- RadioButtonList綁定圖片及泛型Dictionary應(yīng)用
- asp dictionary對(duì)象的用法
- Dictionary擴(kuò)展基礎(chǔ)類向字典中添加鍵和值