关于hastable的问题

EveryCase 2009-08-06 04:56:18
Basically, when you add a key/value pair to a Hashtable or Dictionary object, a hash code
for the key object is obtained first. This hash code indicates which "bucket" the key/value
pair should be stored in. When the Hashtable/Dictionary object needs to look up a key,
it gets the hash code for the specified key object. This code identifies the "bucket" that is
now searched sequentially, looking for a stored key object that is equal to the specified key
object. Using this algorithm of storing and looking up keys means that if you change a key object
that is in a Hashtable/Dictionary, the Hashtable/Dictionary will no longer be able to find
the object. If you intend to change a key object in a hash table, you should remove the original
object/value pair, modify the key object, and then add the new key object/value pair back
into the hash table.

------有点疑惑。想问一下。上文中说的修改键值的时候为什么需要先删除,再添加呢。为什么不能直接修改呢。我没有理解这是为什么呢?
...全文
165 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
EveryCase 2009-08-07
  • 打赏
  • 举报
回复
en 。谢谢大家。我知道了。我把key object 理解成键值对了。结果造成了理解上的困惑。嘎嘎。现在明白了。谢谢!当时看的时候有点大意了。嘎嘎。还有这句话的中文版翻译是“键对”(我在书上看到的)。结果理解成键值了。嘎嘎!!
qqiuzaihui 2009-08-06
  • 打赏
  • 举报
回复
先顶, 明天再看.
  • 打赏
  • 举报
回复
Using this algorithm of storing and looking up keys means that if you change a key objectthat is in a Hashtable/Dictionary, the Hashtable/Dictionary will no longer be able to find
the object.
因为你改变了这个物体,它的key就变了,你就会找不到这个物体了。
所以……
If you intend to change a key object in a hash table, you should remove the original
object/value pair, modify the key object, and then add the new key object/value pair back
into the hash table.

[Quote=引用楼主 everycase 的回复:]
Basically, when you add a key/value pair to a Hashtable or Dictionary object, a hash code
for the key object is obtained first. This hash code indicates which "bucket" the key/value
pair should be stored in. When the Hashtable/Dictionary object needs to look up a key,
it gets the hash code for the specified key object. This code identifies the "bucket" that is
now searched sequentially, looking for a stored key object that is equal to the specified key
object. Using this algorithm of storing and looking up keys means that if you change a key object
that is in a Hashtable/Dictionary, the Hashtable/Dictionary will no longer be able to find
the object. If you intend to change a key object in a hash table, you should remove the original
object/value pair, modify the key object, and then add the new key object/value pair back
into the hash table.

------有点疑惑。想问一下。上文中说的修改键值的时候为什么需要先删除,再添加呢。为什么不能直接修改呢。我没有理解这是为什么呢?
[/Quote]
wuyq11 2009-08-06
  • 打赏
  • 举报
回复
Hashtable HS = new Hashtable();
HS.Add(1, "a");
HS.Add(2, "b");
foreach (DictionaryEntry de in HS)
{
Console.WriteLine(de.Value.ToString());
Console.WriteLine(de.Key.ToString());
}
Hashtable保存的是以(键/值)为一对插入的
添加到Hashtable不能重复,必须有唯一性.并且因为Hashtable添加/删除是无序的,所以速度要快
vwxyzh 2009-08-06
  • 打赏
  • 举报
回复
[Quote=引用楼主 everycase 的回复:]
If you intend to change a key object in a hash table, you should remove the original
object/value pair, modify the key object, and then add the new key object/value pair back
into the hash table.
[/Quote]
lz,先看清楚前提条件
四更山吐月 2009-08-06
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 wf5360308 的回复:]
键名是不可以直接修改的!
只有删除后重新添加!
[/Quote]
冷月孤峰 2009-08-06
  • 打赏
  • 举报
回复
键名是不可以直接修改的!
只有删除后重新添加!
shalen520 2009-08-06
  • 打赏
  • 举报
回复
修改键名key的时候,当然要先移除了,因为新的key和原先的key hash值不一样,需要重新计算

修改键值就不用了,因为一般存的是引用
hecker728 2009-08-06
  • 打赏
  • 举报
回复
看下,怎么对哈希表排序,就明白了

对哈希表进行排序

对哈希表进行排序在这里的定义是对key/键值对中的key按一定规则重新排列,但是实际上这个定义是不能实现的,因为我们无法直接在Hashtable进行对key进行重新排列,如果需要Hashtable提供某种规则的输出,可以采用一种变通的做法:
ArrayList akeys=new ArrayList(ht.Keys); //别忘了导入System.Collections
akeys.Sort(); //按字母顺序进行排序
foreach(string skey in akeys)
{
Console.Write(skey ":");
Console.WriteLine(ht[skey]);//排序后输出
}

注意:用foreach
下面是一个简单的例子:
class SampleHashtable
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
// Create and initialize a new Hashtable.
Hashtable table = new Hashtable();
//Student Name, Grade
table.Add("leiwanjun", 100);
table.Add("wanghuan", 87);
table.Add("wuhailong", 92);
table.Add("renyao", 76);
table.Add("tanghonglei", 84);
table.Add("chenxiaoping", 91);
table.Add("liupeixun", 80);
table.Add("huyoumou", 87);
// Display the properties and values of the Hashtable.
Console.WriteLine("Count: {0}", table.Count);
PrintTable(table);
Console.WriteLine();
int g = (int) table["wuhailong"];
Console.WriteLine ("wuhailong's grade is: {0}", g);
Console.WriteLine();
PrintItems ("All Names", table.Keys);

Console.WriteLine();
PrintItems ("All Grades", table.Values);
}
public static void PrintTable( Hashtable myList)
{
Console.WriteLine ("{0, -15} {1, -15}", "Name","Grade");
Console.WriteLine ("{0, -15} {1, -15}", "----","-----");
// // 排序
// ArrayList al = new ArrayList(myList.Keys);
// al.Sort();
// foreach (string Name in al)
// {
// Console.WriteLine("{0, -15} {1, -15}", Name, myList[Name]);
// }
// //
//遍历哈希表中的每个元素,直接输出
foreach (DictionaryEntry e in myList)
{
Console.WriteLine ("{0, -15} {1, -15}", e.Key, e.Value);
}
}
public static void PrintItems(string title, IEnumerable myList )
{
Console.Write ("{0}: ", title);
StringBuilder sb = new StringBuilder();
foreach (object o in myList)
{
sb.AppendFormat( "{0}, ", o);
}
sb.Remove(sb.Length - 2, 2);
Console.WriteLine(sb);
dancingbit 2009-08-06
  • 打赏
  • 举报
回复
值有变化,那么对应的Hash值也会有变化,Hashtable的实现中可能只在添加的时候才会重新计算Hash值并排列好位置(如果中间更改的代价太大,这样做是合理的),所以会有这样的要求。
prometheusstar163 2009-08-06
  • 打赏
  • 举报
回复
hash表中的key排序是hash排序,所以叫hashtable,如果你直接修改我估计是不会进行重新hash排序的,必须添加,这样能重新排序,否则查找会出错
NealXX 2009-08-06
  • 打赏
  • 举报
回复
路过 帮顶

111,092

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧