【问题标题】:C# Dealing with DictionariesC# 处理字典
【发布时间】:2017-02-13 03:08:50
【问题描述】:

我是使用 c# 的新手,我有使用 c++ 和 java 的经验。我试图弄乱字典,但我真的不能让它工作。我有两个数组,数据类型必须是对象,在我将它们添加到两个不同的字典后,我试图在其中找到一个键,但我无法让它进入 if 语句。字典的两个声明中哪个是正确的 dictionary1还是字典2?另外,我如何通过键或通过正确字典中的值或两者找到键来查找值。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Practice_With_Dictionaries
{
    class Program
    {
        static void Main(string[] args)
        {


                object[] array1 = new object[5];
                array1[0] = "1111";
                array1[1] = "2222";
                array1[2] = "3333";
                array1[3] = "4444";
                array1[4] = "5555";

                object[] speed = new object[5];
                speed[0] = 1;
                speed[1] = 2;
                speed[2] = 3;
                speed[3] = 4;
                speed[4] = 5;

                object[] keys = new object[1];
                keys[0] = (object[])array1;

                object[] speedTable = new object[1];
                speedTable[0] = (object[])speed;


                Dictionary<object, object> dictionary1 = new Dictionary<object, object>();
                Dictionary<object[], object[]> dictionary2 = new Dictionary<object[], object[]>();


                dictionary1.Add(keys, speedTable);
                dictionary2.Add(keys, speedTable);

                if (dictionary1.ContainsKey((object)"1111"))
                {
                    var method = 1;
                }

                if (dictionary2.ContainsKey(array1))
                {
                    var method = 2;
                }



        }
    }
}

【问题讨论】:

  • 也许您应该考虑使用Int32String 作为键,而不是Object

标签: c# object dictionary c#-4.0


【解决方案1】:

dictionary1.ContainsKey((object)"1111") 永远不会返回 true,因为 "1111" 每次都会被装箱到一个新的唯一对象中。

一次填充一项

您可以一次在字典中填充一项:

    Dictionary<object, object> dictionary1 = new Dictionary<object, object>();

    for (int i = 0; i < array1.Length; i++)
    {
        dictionary1.Add(array1[i], speed[i]);
    }

    object key1 = array1[0];

    if (dictionary1.ContainsKey(key1))
    {
        var method = 1;
    }

使用 LINQ 填充

您还可以使用 LINQ 和 ToDictionary(IEnumerable<TSource, Func<TSource, TKey>, Func<TSource, TElement>) 方法填充字典而无需显式循环,该方法根据指定的键选择器和元素选择器函数从 IEnumerable 创建字典。

Dictionary<object, object> dictionary2 = array1
    .Select((obj, index) => new KeyValuePair<object, object>(array1[index], speed[index]))
    .ToDictionary(kvp => kvp.Key, kvp => kvp.Value);

Dictionary<object, object> dictionary3 = array1
    .Select((obj, index) => index)
    .ToDictionary(i => array1[i], i => speed[i]);

Dictionary<object, object> dictionary4 = Enumerable.Range(0,5)
    .ToDictionary(i => array1[i], i => speed[i]);

【讨论】:

    【解决方案2】:

    您的代码面临的挑战是您以数组或其他形式传递键值,它们使用列表。通常我们以一对一的关系初始化一个字典:

    Dictionary<object, object> dict = new Dictionary<object, object>();
    

    有时是一对多的关系:

    Dictionary<object, object[]> dict = new Dictionary<object, object[]>();
    

    在您的情况下,您将其初始化为多对多关系:

    Dictionary<object[], object[]> dictionary2 = new Dictionary<object[], object[]>();
    

    尽管在您的第一个示例中,尤其是在字典 1 中,您仍然在 TKey 值上传递了一个数组(请参阅您的代码):

    Dictionary<object, object> dictionary1 = new Dictionary<object, object>();
    dictionary1.Add(keys, speedTable); //the value of keys consists of an object of an array: keys[0] = (object[])array1;
    

    因此,最好的办法是使用单个对象的 TKey 和对象或对象数组或对象列表的 TValue 来实现 Dictionary。

    如果您仍然想做一个对象数组,您需要实现一个自定义 IEqualityComparer,因为您无法在 if 语句中执行您想要执行的操作。

    这是一个示例通用实现,您需要在构造函数 Dictionary 中提供 IEqualityComparer:

     public class DictionaryComparer<T> : IEqualityComparer<List<T>>
     {
        public bool Equals(List<T> x, List<T> y)
        {
            //TODO: Your implementation for your contains or equals condition
        }
    
        public int GetHashCode(List<T> obj)
        {
          //TODO: Implementation of your GetHashCode
        }
     }
    

    然后实现它:

     if (new DictionaryComparer<object>().Equals(lstCompare, lstCompareTo))
       {
              //TODO: Your condition here..
       }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-27
      • 1970-01-01
      相关资源
      最近更新 更多