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

问题描述

朋友,
我有一个哈希表,我想比较存储在其中的值.
例如

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..

推荐答案

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
}



现在,看看我对这个问题的评论.您没有完全描述所需的比较.假设您要检查所有值是否唯一.现在,请记住字典的属性.所有键都是唯一的,对.因此,您可以创建另一个字典,然后使用您的值作为键.如果将所有值添加为字典的键,则此值是唯一的.

如果您想要其他东西(看,您确实应该在问题中寻找歧义!)—没问题:我向您介绍了如何遍历所有成员,以便您可以自己实现其余的内容.

-SA



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.

—SA


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;
    }
  }


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

07-23 13:50