Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何交换哈希表';有价值的钥匙_C#_.net_Hashtable - Fatal编程技术网

C# 如何交换哈希表';有价值的钥匙

C# 如何交换哈希表';有价值的钥匙,c#,.net,hashtable,C#,.net,Hashtable,我通过以下方法解决了这个问题。 因为很长,我需要一个更好的 //代码 class Program { static void Main(string[] args) { Hashtable hsTbl = new Hashtable(); hsTbl.Add(1, "Suhas"); hsTbl.Add(2, "Madhuri"); hsTbl.Add(3, "Om"); List<objec

我通过以下方法解决了这个问题。 因为很长,我需要一个更好的

//代码

class Program
{
    static void Main(string[] args)
    {
        Hashtable hsTbl = new Hashtable();

        hsTbl.Add(1, "Suhas");
        hsTbl.Add(2, "Madhuri");
        hsTbl.Add(3, "Om");
        List<object> keyList = new List<object>();
        List<object> ValList = new List<object>();

        Console.WriteLine("Key          Value");
        foreach (DictionaryEntry item in hsTbl)
        {
            Console.WriteLine(item.Key + "      " + item.Value);
            keyList.Add(item.Value);
            ValList.Add(item.Key);

        }
        hsTbl.Clear()

//Swapping          

        for (int i = 0; i < keyList.Count; i++)
        {
            hsTbl.Add(keyList[i], ValList[i]);
        }

//will display hashtable after swapping

        foreach (DictionaryEntry item in hsTbl)
        {
            Console.WriteLine(item.Key + "      " + item.Value);
        }
    }
}
using System;
using System.Collections;

class Program
{
  static void Main()
  {
      Hashtable hsTbl = new Hashtable();

      hsTbl.Add(1, "Suhas");
      hsTbl.Add(2, "Madhuri");
      hsTbl.Add(3, "Om"); 

      DictionaryEntry[] entries = new DictionaryEntry[hsTbl.Count];
      hsTbl.CopyTo(entries, 0);
      hsTbl.Clear();

      foreach(DictionaryEntry de in entries) hsTbl.Add(de.Value, de.Key);

      // check it worked

      foreach(DictionaryEntry de in hsTbl)
      {
         Console.WriteLine("{0} : {1}", de.Key, de.Value);
      }
  }
}
类程序
{
静态void Main(字符串[]参数)
{
Hashtable hsTbl=新的Hashtable();
hsTbl.添加(1,“Suhas”);
hsTbl.添加(2,“Madhuri”);
hsTbl.添加(3,“Om”);
List keyList=新列表();
List ValList=新列表();
Console.WriteLine(“键值”);
foreach(字典输入项在hsTbl中)
{
Console.WriteLine(item.Key+“”+item.Value);
keyList.Add(item.Value);
ValList.Add(item.Key);
}
hsTbl.Clear()
//交换
for(int i=0;i

还有其他更好的解决方案吗?

您可以使用一个额外的数组和CopyTo方法(而不是两个列表)稍微简单一点,但无需创建额外的哈希表,如下所示:

//代码

class Program
{
    static void Main(string[] args)
    {
        Hashtable hsTbl = new Hashtable();

        hsTbl.Add(1, "Suhas");
        hsTbl.Add(2, "Madhuri");
        hsTbl.Add(3, "Om");
        List<object> keyList = new List<object>();
        List<object> ValList = new List<object>();

        Console.WriteLine("Key          Value");
        foreach (DictionaryEntry item in hsTbl)
        {
            Console.WriteLine(item.Key + "      " + item.Value);
            keyList.Add(item.Value);
            ValList.Add(item.Key);

        }
        hsTbl.Clear()

//Swapping          

        for (int i = 0; i < keyList.Count; i++)
        {
            hsTbl.Add(keyList[i], ValList[i]);
        }

//will display hashtable after swapping

        foreach (DictionaryEntry item in hsTbl)
        {
            Console.WriteLine(item.Key + "      " + item.Value);
        }
    }
}
using System;
using System.Collections;

class Program
{
  static void Main()
  {
      Hashtable hsTbl = new Hashtable();

      hsTbl.Add(1, "Suhas");
      hsTbl.Add(2, "Madhuri");
      hsTbl.Add(3, "Om"); 

      DictionaryEntry[] entries = new DictionaryEntry[hsTbl.Count];
      hsTbl.CopyTo(entries, 0);
      hsTbl.Clear();

      foreach(DictionaryEntry de in entries) hsTbl.Add(de.Value, de.Key);

      // check it worked

      foreach(DictionaryEntry de in hsTbl)
      {
         Console.WriteLine("{0} : {1}", de.Key, de.Value);
      }
  }
}

请注意,通常情况下,无法保证任何方法都能工作,因为原始哈希表中的某些值可能会重复,因此不适合用作键。

您可以使用通用
字典
,这很容易用Linq创建:

var dictionary = hsTbl.OfType<DictionaryEntry>()
                      .ToDictionary(e => (string)e.Value, e => (int)e.Key);

但是,如果要交换值和键,请确保所有值都是唯一的。

使用哈希表而不是通用字典的原因是什么?
只需在foreach循环中创建一个以键为值、以值为键的新列表,然后忘记旧列表。是否需要交换?是否需要考虑特定键的值可能与同一集合中的另一个值相同的情况?此外,您可能需要考虑使用字典来进行类型安全性和性能背后的原因。刚开始。