濮阳杆衣贸易有限公司

主頁 > 知識(shí)庫 > 自寫一個(gè)模仿Dictionary與Foreach的實(shí)現(xiàn)及心得總結(jié)

自寫一個(gè)模仿Dictionary與Foreach的實(shí)現(xiàn)及心得總結(jié)

熱門標(biāo)簽:百度ai地圖標(biāo)注 電話機(jī)器人軟件銷售工作 南陽外呼系統(tǒng)定制化 玉林市機(jī)器人外呼系統(tǒng)哪家好 同安公安400電話怎么申請(qǐng)流程 蘋果手機(jī)凱立德地圖標(biāo)注 預(yù)測(cè)式外呼系統(tǒng)使用說明 合肥電銷外呼系統(tǒng)哪家公司做的好 申請(qǐng)400電話手續(xù)
自己寫一個(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ǔ)類向字典中添加鍵和值

標(biāo)簽:海南 臺(tái)州 南京 南昌 揚(yáng)州 南京 嘉興 淄博

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《自寫一個(gè)模仿Dictionary與Foreach的實(shí)現(xiàn)及心得總結(jié)》,本文關(guān)鍵詞  自寫,一個(gè),模仿,Dictionary,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《自寫一個(gè)模仿Dictionary與Foreach的實(shí)現(xiàn)及心得總結(jié)》相關(guān)的同類信息!
  • 本頁收集關(guān)于自寫一個(gè)模仿Dictionary與Foreach的實(shí)現(xiàn)及心得總結(jié)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    扶沟县| 桐梓县| 郴州市| 甘孜| 旅游| 图木舒克市| 晴隆县| 石阡县| 成武县| 长治县| 兰坪| 台安县| 克拉玛依市| 静海县| 新巴尔虎右旗| 阿荣旗| 宜都市| 五河县| 牙克石市| 宜宾市| 巴东县| 镇安县| 海城市| 阜宁县| 庄河市| 尉氏县| 遵义市| 禹城市| 石狮市| 宁河县| 莲花县| 饶平县| 巨野县| 湘乡市| 黔江区| 新余市| 桦川县| 博白县| 历史| 叶城县| 剑阁县|