哈希表 [英] HashTable

查看:71
本文介绍了哈希表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


如果我在HashTable中插入一组键和相关值,我该如何
提取给定值对于指定的键?


Thanx,

比尔

Hi y''all,

If I insert a set of keys and associated values in a HashTable, how do I
extract a given value for a specified key?

Thanx,
Bill

推荐答案

string myValue =(string)this.myHash [myKey];


....当然,假设你的值是一个字符串。它可以是

任何东西(在这种情况下,替换string定义和

"(字符串)"使用正确的类型为你的价值)。


您需要转换,因为Hashtable []在.NET v1.1中返回一个Object,

所以你必须将Object转换为正确的价值类型。在

..NET v2.0中,您将能够创建仅包含特定

值的Hashtables,因此如果您声明,则不需要演员表哈希表

正确。但是现在,你仍然不得不施展。


如果你需要遍历Hashtable中的所有键和值,

你是这样做的:


foreach(myHashtable中的DictionaryEntry de)

{

string key =(string)de。 Key;

string val =(string)de.Value;

...用key和val做点什么...

}


同样,key可以是任何类型,val可以是任何类型......相应地更改

定义和强制转换。
string myValue = (string)this.myHash[myKey];

....that is assuming, of course, that your value is a string. It can be
anything at all (in which case, replace the "string" definition and the
"(string)" cast with the correct type for your value).

You need the cast because Hashtable[] returns an Object in .NET v1.1,
so you have to cast the Object to the correct type for your value. In
..NET v2.0 you''ll be able to create Hashtables that hold only particular
values, so you won''t need the cast if you declare the hash table
properly. For now, though, you''re stuck with having to cast.

If you ever need to iterate through all keys and values in a Hashtable,
you do that like this:

foreach (DictionaryEntry de in myHashtable)
{
string key = (string)de.Key;
string val = (string)de.Value;
... do something with key and val ...
}

Again, key can be any type and val can be any type... change the
definitions and the casts accordingly.


您可以使用ht.ContainsKey来查看密钥是否存在以及索引器ht [key]

来提取值。

------

Ajay Kalra
aj *** ****@yahoo.com

You can use ht.ContainsKey to see if key exists and the indexer ht[key]
to extract the value.

------
Ajay Kalra
aj*******@yahoo.com


这篇关于哈希表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆