【问题标题】:Dictionary To list issue字典列出问题
【发布时间】:2014-02-24 14:15:46
【问题描述】:

我有以下字典,我正在填写通过第三方对象解析的数据。

在我的解析类中看起来像这样:

public string beaconMajor;
public string[] beaconValues;
public Dictionary<string, float> valueDictionary = new Dictionary<string, float>();
public string foo = "";

public void DistanceValue(string message)
{

    foo = message.Split (',');

    foreach (string b in foo)
    {
         beaconValues = beacon.Split('_');

        beaconMajor = beaconValues[0];
        string beaconDistance = beaconValues[1];

        if(valueDictionary.ContainsKey(beaconMajor))
        {
            valueDictionary[beaconMajor] = float.Parse(beaconDistance);
        }
        else
        {
            valueDictionary.Add(beaconMajor, float.Parse(beaconDistance));
        }
    }

    if (foo.Contains("-1")) 
    {
        return;
    }
}

然后在另一堂课中,我正在使用字典执行以下操作,只会向您显示一个条目,但有很多看起来像这样:

if (foo_class.valueDictionary.ContainsKey ("key"))
{
    beacon1Scale = float.Parse (foo_class.valueDictionary["key"].ToString());
}

现在我想做的是按字典的值而不是键对字典进行排序。键是常量,但值每隔几秒就会改变一次。我试过以下代码:

List<KeyValuePair<string, float>> list = foo_class.valueDictionary.ToList();

list.Sort
(delegate(KeyValuePair<string, float> val1, KeyValuePair<string, float> val2)
    {
        return val1.Value.CompareTo(val2.Value);
    }
);

Debug.Log(list);

dictionaryString = list.ToString() + "\n";

但是当我运行它时,我在 xCode 中收到以下消息:

System.Collections.Generic.List1[System.Collections.Generic.KeyValuePair2[System.String,System.Single]] UnityEngine.Debug:Internal_Log(Int32, String, Object) UnityEngine.Debug:Log(Object) TriangulationScript:Update()

有人可以告诉我此消息的原因吗?

我仍在研究让这个字典通过值对自身进行排序,我什至尝试过使用 LINQ,但这与 xCode 不兼容,并且我在 Xcode 中出现 JIT 错误消息,如下所示:

ExecutionEngineException:正在尝试 JIT 编译方法 'System.Linq.OrderedEnumerable1:GetEnumerator ()' 使用 --aot-only 运行时。

如果有人知道任何其他方法可以做到这一点,请告诉我。我现在已经花了几天时间。

更新

我使用了下面建议的答案,并在 xcode 中收到了这条消息:

ExecutionEngineException:正在尝试 JIT 编译方法 'System.Linq.OrderedEnumerable1&lt;System.Collections.Generic.KeyValuePair2>:GetEnumerator ()' 同时使用 --aot-only 运行。

在 System.Collections.Generic.List1[System.Collections.Generic.KeyValuePair2[System.String,System.Single]].AddEnumerable (IEnumerable1 enumerable) [0x00000] in <filename unknown>:0 at System.Collections.Generic.List1[System.Collections.Generic.KeyValuePair2[System.String,System.Single]]..ctor (IEnumerable1 集合)[0x00000] in :0 at System.Linq.Enumerable.ToList[KeyValuePair2] (IEnumerable1 来源) [0x00000] in :0 at TriangulationScript.Update () [0x00000] 在 :0

【问题讨论】:

  • 回滚编辑,因为上述代码需要在 xCode 中编译,这就是我不能使用 LINQ 的原因。如果有人知道这样做的方法,请告诉我。
  • OT:你如何在 xcode 中使用 c#?
  • xCode 插件与 Unity。我所有的 C# 都是在 Unity 方面完成的。

标签: c# xcode linq dictionary


【解决方案1】:

list.ToString() 并没有像您认为的那样做 - 看起来您希望 list.ToString() 将整个列表的内容输出到一个字符串。相反,它使用object.ToString() 的默认行为,即发出对象的完全限定类型名称——在本例中为

System.Collections.Generic.List`1[System.Collections.Generic.KeyValuePair`2[System.String,System.Single]]

我仍在研究如何让这本字典通过值自行排序。

A Dictionary 没有 顺序 - 如果您想以特定顺序输出 kay/value 对,则每次遍历列表时都必须指定该顺序, 或存储为键/值对列表,它不会提供Dictionary 特定的方法(如索引查找)

【讨论】:

    【解决方案2】:

    试试这样的:

    List<KeyValuePair<string, float>> list = items
                                              .OrderBy(x => x.Value)
                                              .ToList<KeyValuePair<string, float>>();
    

    【讨论】:

    • OrderBy 是 Linq,我以前用过。这就是导致 JIT 消息配对的原因。
    • 然后,尝试使用我的 github repo 的某种算法。 github.com/Oscarbralo/Algorithms 但是您需要将它们与字典一起使用;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-21
    • 1970-01-01
    • 2022-07-05
    • 2023-03-11
    • 2016-06-09
    • 2018-03-26
    • 1970-01-01
    相关资源
    最近更新 更多