遍历哈希表 [英] Loop through Hashtable
问题描述
朋友,
我有一个哈希表,我想比较存储在其中的值.
例如
Hi Friends,
I have a Hashtable and I want to compare the values stored in it.
e.g
Hastable ht= new Hashtable();
它的集合为
and it has collection as
Key Value
Dropdown -- 1000
Dropdown -- 2000
Dropdwon -- 3000
我想比较Hashtabale中的值
如果它们不相同,则返回false;
我怎么能得到这个?
我的意思是这样的...
I want to compare the values in Hashtabale
if they are not same, return false;
How can I get this??
I mean something like this...
for(i=0;i<ht.count;i++)
{
if(ht[i].value==ht[i+1].value)
flag=true;
else
flag=false;
}
谢谢,
Lok ..
Thanks,
Lok..
推荐答案
由于泛型最早是在.NET Framework v.2.0中引入的,因此使所有非泛型集合类型(如Hashtable
)都已过时.这些类型未标记为不推荐使用,以避免破坏与旧代码的兼容性,但是对于新开发,仅使用通用类型是有意义的.
可以使用System.Collections.Generic
中的类型来代替Hashtable
.您可以选择System.Collections.Generic.Dictionary<TKey, TValue>
,System.Collections.Generic.SortedDictionary<TKey, TValue>
或System.Collections.Generic.SortedDictionary<TKey, TValue>
,请参见:
http://msdn.microsoft.com/en-us/library/xfhwa508.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/f7fta44c.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/ms132319.aspx [ ^ ].
它们具有相同的接口,不同之处在于内存消耗冗余和性能之间的差异平衡.
还有什么?啊,循环播放...难道不是吗?它们都实现了接口IEnumerable<KeyValuePair<TKey, TValue>>
,这意味着……什么?对,这意味着您可以使用foreach
:
As the generics were introduced as early as in .NET Framework v.2.0, it made all non-generic collection types likeHashtable
obsolete. Those types were not marked deprecated in order to avoid breaking compatibility with legacy code, but for new development it makes sense to use only generic types.
Instead ofHashtable
, you can use the types fromSystem.Collections.Generic
. You have a choice fromSystem.Collections.Generic.Dictionary<TKey, TValue>
,System.Collections.Generic.SortedDictionary<TKey, TValue>
orSystem.Collections.Generic.SortedDictionary<TKey, TValue>
, please see:
http://msdn.microsoft.com/en-us/library/xfhwa508.aspx[^],
http://msdn.microsoft.com/en-us/library/f7fta44c.aspx[^],
http://msdn.microsoft.com/en-us/library/ms132319.aspx[^].
They have identical interface, the difference is the difference balance between redundancy in memory consumption and performance.
What else? Ah, looping… Isn''t it easy? They all implement the interfaceIEnumerable<KeyValuePair<TKey, TValue>>
and it means… what? Right, it means that you can useforeach
:
foreach (KeyValuePair<TKey, TValue> pair in myCollection) {
//for example:
TKey key = pair.Key; //use it the way you want
TValue value = pair.Value; //use it the way you want
}
现在,看看我对这个问题的评论.您没有完全描述所需的比较.假设您要检查所有值是否唯一.现在,请记住字典的属性.所有键都是唯一的,对.因此,您可以创建另一个字典,然后使用您的值作为键.如果将所有值添加为字典的键,则此值是唯一的.
如果您想要其他东西(看,您确实应该在问题中寻找歧义!)—没问题:我向您介绍了如何遍历所有成员,以便您可以自己实现其余的内容.
Now, look at my comment to the question. You did not fully describe required comparison. Let''s assume you want to check up if all values are unique or not. Now, remember the properties of dictionary. All key are unique, right. So, you can create yes another dictionary and use your values as keys. If you add all values as keys to your dictionary, every value is unique is this set.
If you wanted something else (look, you should really look for ambiguities in your questions!) — not a problem: I explained you how to iterate through all members, so you can implement the rest by yourself.
如果需要检查任何哈希表值是否相等,则可以使用Values.CopyTo
方法,然后使用普通的for
循环,例如
If you need to check if any of the hashtable values are equal then you could use theValues.CopyTo
method and then work with the array using the ordinaryfor
loop, e.g.
int[] a = new int[hashtable.Values.Count];
hashtable.Values.CopyTo(a,0);
bool flag = false;
for (int i = 0; i < a.Length-1; i++)
for (int j = i + 1; j < a.Length; j++)
{
if (a[i] == a[j])
{
flag = true;
break;
}
}
这篇关于遍历哈希表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!