【问题标题】:How do I remove element from specific place in Dictionary in C#?如何从 C# 字典中的特定位置删除元素?
【发布时间】:2010-12-23 05:44:53
【问题描述】:

我有一个“字典”数据库,我想从特定位置返回一个元素。我看到有“ElementAt”功能,但我没有设法使用它。

为什么这样的东西不起作用?

closeHash.ElementAt<State>(i);

它告诉我以下错误:

错误 3 'System.Collections.Generic.Dictionary' 不包含 'ElementAt' 的定义,并且最佳扩展方法重载 'System.Linq.Queryable.ElementAt(System.Linq.IQueryable, int)' 有一些无效论据

而且这段代码也不起作用,因为 closeHash[i] 只给我索引而不是实际元素:

   if (closeHash.ContainsKey(i) && ((State)closeHash[i]).getH() + 
((State)closeHash[i]).getG() > checkState.getH() + checkState.getG()

Dictionary 中的每个元素都属于“State”类,而 checkState 也是具有 GetH 和 GetG 函数的 State。我想取出第 I 个位置的元素并对其进行处理,而不仅仅是删除它。

提前致谢!

格雷格

【问题讨论】:

  • “特定位置”是什么意思?字典是无序的 - 您可以使用 SortedDictionary 或 SortedList 代替吗?

标签: c# hash dictionary element


【解决方案1】:

我相信您可以通过某种方式做到这一点,但哈希表类型的集合通常不适用于“顺序”的概念。在 Java 中,您可以获取 Enumerator 或 Iterator 并删除您遇到的第 n 个项目,但同样,我认为这没有意义。

【讨论】:

    【解决方案2】:

    您只需要使用 System.Linq 就可以使用 ElementAt 扩展方法。将其包含在类声明的开头:

    using System.Linq;
    

    这应该可以解决问题。

    【讨论】:

      【解决方案3】:

      使用 Remove 函数并传入 ElementAt 怎么样?

              Dictionary<int, string> closeHash = new Dictionary<int, string>();
              closeHash.Add(47, "Hello");
              closeHash.Remove(closeHash.ElementAt(0).Key);
      

      【讨论】:

        【解决方案4】:

        错误消息暗示您的变量 closeHash 是一个字典,显然是字典。如果不是,请注明字典的确切声明和“i”。

        那么closeHash[i] 应该给出一个State类型的值,所以你不需要强制转换。

        正如其他人所说,字典没有“顺序”的概念,因此没有“第 n 项”的概念。

        【讨论】:

          【解决方案5】:

          使用通用集合中的字典,您永远不必使用 RemoveAt()。字典中的键值必须是唯一的。

          //       Unique Not Unique
          //          |     |   
          Dictionary<int, string> alphabet = new Dictionary<int, string>();
          alphabet.Add(1, "A");
          //Adding this will cause an Argument Exception to be thrown
          //The message will be: An item with the same key has already been added.
          alphabet.Add(1, "A");
          

          如果我想从我的字母示例中删除键为 24 的项目,这就是我需要的:

          alphabet.Remove(24)
          

          这是可行的,因为永远不会有 2 个具有相同值的键。

          现在,如果您想在不知道它是关键的情况下删除一个项目,那就是另一回事了。您需要遍历每个元素并尝试找到与之关联的键。我会使用 linq,有点像这样:

          var key = (from item in alphabet
                       where item.Value == "K"
                       select item.Key).FirstOrDefault();
          //Checking to make sure key is not null here
          ...
          //Now remove the key
          alphabet.Remove(key)
          

          无论是哪种方式,我都看不出有任何理由需要从键值必须唯一的任何列表中删除At(index)。

          【讨论】:

          • 重新“确保密钥不为空”,您需要如下代码:int? key = alphabet.Where( item =&gt; item.Value == "K" ).Select( item =&gt; (int?)item.Key ).FirstOrDefault( ); if( key.HasValue ) { alphabet.Remove( key.Value ); }
          • 如果您在键上使用引用类型,我认为您应该按照答案中的说明对键进行浅拷贝。如果你使用深拷贝对象,那么我认为.Remove 无法工作,因为我没有测试过。
          猜你喜欢
          • 2021-10-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-12-17
          • 1970-01-01
          相关资源
          最近更新 更多