2017-02-13 110 views
0

我与c#一起工作,我有使用C++和java的经验。我试图弄乱字典,但我真的无法得到这个工作。我有两个数组的数据类型必须是对象,后我将它们添加到两个不同的字典,即时尝试找到一个关键,但我不能让它进入if语句。这两个字典的声明是正确的字典1或dictionary2?另外我怎样才能找到一个价值的关键或由正确的词典中的价值或两者兼而有之。因为"1111"每次都会被装箱到一个新的唯一对象C#处理字典

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



     } 
    } 
} 
+0

也许你应该考虑使用'Int32'或'String'作为键,而不是“对象”? –

回答

1

dictionary1.ContainsKey((object)"1111")永远不会返回true。同时

填充一个项目

可以填充在一个时间字典一个项目:使用LINQ

您还可以填充不使用LINQ显式循环字典

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

填充和ToDictionary(IEnumerable<TSource, Func<TSource, TKey>, Func<TSource, TElement>)方法,该方法根据指定的键选择器和元素选择器函数从IEnumerable创建一个Dictionary。

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]); 

0

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

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

有时在一个一对多的关系:

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

在你的情况,你初始化一个多对多的关系:

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

虽然在第一个例子中,尤其是dictionary1中,您仍然在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语句做。

下面是一个简单通用的实现,需要在构造函数字典提供的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.. 
    }