using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace _EditDictionary
{
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 ContainKey(string key)
{
bool result=false;
foreach(KeyValuePair kvp in list)
{
if (kvp.Key == key)
{
result = true;
// value += kvp.Value ;
break;
}
}
return result;
}
}
class Program
{
static void Main(string[] args)
{
Mydic dic = new Mydic();
string[] arr = File.ReadAllLines(@"D:\英汉词典TXT格式.txt", Encoding.Default);//第二个参数是防止乱码;第一个参数是字典的txt文档(找一个大的txt字典,测试程序才能更有效)
foreach(string item in arr)
{
//分割出单词和解释
string[] strArr = item.Split(new char[]{' '},StringSplitOptions.RemoveEmptyEntries);
if (strArr.Length == 2)
{
if(!dic.ContainKey(strArr[0]))
{
dic.Add(strArr[0],strArr[1]);
}
}
}
Stopwatch sw = new Stopwatch();
sw.Start();
dic.ContainKey("china");
sw.Stop();
Console.WriteLine(sw.Elapsed);
Console.WriteLine();
Console.Read();
}
}
}
耗时: