求一个使用哈希的例子
解决方案 »
- 如何设置DataGridView中文字的行距
- visio二次开发
- 还是上传文题
- Excel文件导入
- 老是说参数不对,请教一个Win32 API在C#中引用的写法!
- 数据库的事务问题
- dll是自己写的,dll项目和主项目在同一个解决方案下,我想点F12定义到dll源码,但他总是转到从元数据,很不方便,请问怎么设置能按f12是定位到dll源码
- 创建文件写入字符串的问题
- 如何控制一个事件处理函数的触发依次的被处理?
- WPF中,datagrid如何合并单元格
- 用Reflector反编译有些发布的dll里面的方法看不到东西这个是怎么实现的?
- 数据用dephi写的DES加密,现在用C#解密需要初始向量。怎么办?
using System.Collections; //使用Hashtable时,必须引入这个命名空间class hashtable
{
public static void Main()
{
Hashtable ht=new Hashtable(); //创建一个Hashtable实例
ht.Add("E","eeee"); //添加key/value键值对
ht.Add("A","aaaa");
ht.Add("C","cccc");
ht.Add("B","bbbb"); //遍历哈希表需要用到 DictionaryEntry Object
foreach(DictionaryEntry de in ht) //ht为一个Hashtable实例
{
Console.Write(de.Key + " "); //de.Key对应于key/value键值对key
Console.WriteLine(de.Value); //de.Key对应于key/value键值对value
} //对哈希表进行排序
ArrayList akeys=new ArrayList(ht.Keys);
akeys.Sort(); //按字母顺序进行排序
foreach (string skey in akeys)
{
Console.Write(skey + ":");
Console.WriteLine(ht[skey]); //排序后输出
} string s=(string)ht["A"];
Console.WriteLine(s); if(ht.Contains("E")) //判断哈希表是否包含特定键,其返回值为true或false
Console.WriteLine("the E key:exist"); ht.Remove("C"); //移除一个key/value键值对
Console.WriteLine(ht["A"]); //此处输出a
ht.Clear(); //移除所有元素
Console.WriteLine(ht["A"]); //此处将不会有任何输出 Console.ReadKey();
}
}
table.Add(1, "A");
table.Add(2, "B");
table.Add(3, "C");table.Remove(2);
foreach (Object o in table.Keys) {
Console.WriteLine(o.ToString());
}
foreach (DictionaryEntry d in table) {
Console.WriteLine ("{0}\t{1}", d.Key, d.Value);
}
//Hashtable 表示键/值对的集合,这些键/值对根据键的哈希代码进行组织。
Hashtable ht = new Hashtable();
ht.Add(1, null);
ht.Add(2, "2");
ht.Add(3, "3");
ht.Add(4, "4"); Console.WriteLine("\nht.ContainsKey(3) = {0}\tht.ContainsValue(\"3\") = {1}", ht.ContainsKey(3), ht.ContainsValue("3"));
//System.Collections.IEnumerator ien = ht.GetEnumerator(); //ht.Remove(1); //移除键
//ht.Clear(); //清除所有的键 //1
foreach (int item in ht.Keys )
{
Console.WriteLine("key = {0}\t value = {1}", item, ht[item]);
} //2
foreach (DictionaryEntry item in ht) //DictionaryEntry 定义可设置或检索的字典键/值对。
{
Console.WriteLine("{0} {1}", item.Key, item.Value);
} Console.WriteLine("-------------------------------------------------");
ICollection valuecoll = ht.Values; //ICollection 定义所有非泛型集合的大小、枚举数和同步方法。
Console.WriteLine("valuecoll.Count = {0}", valuecoll.Count.ToString()); //valuecoll.Count = 4 foreach (string item in valuecoll)
{
Console.WriteLine("value = {0}", item);
} Console.WriteLine();
ICollection keycoll = ht.Keys;
foreach (int item in keycoll )
{
Console.WriteLine("key = {0}", item);
}
Console.WriteLine("-------------------------------------------------");