[英]Update key value dictionary list?
Ok, there are answers for doing this with normal dictionary but the size of my dictionary / things to be stored is constantly changing, so I thought use a list. 好的,对于普通词典,这样做有答案,但是我的词典/要存储的内容的大小在不断变化,因此我想使用一个列表。 Here is what Im storing: 这是即时消息存储的内容:
public List<KeyValuePair<GameObject, float>> planesFilled;
I have to continually update the values (and add pairs if they spawn and don't already exist in the list in my update function. I am storing the distance between the objects and another object in scene. 我必须不断更新这些值(如果它们在我的更新函数中的列表中生成且还不存在,则添加对。我正在存储对象与场景中另一个对象之间的距离。
I am having errors relating to not being able to call Contains
for just a key - I dont necessarily know the past value. 我遇到与无法仅通过键调用Contains
有关的错误-我不一定知道过去的值。 My key should be the Gameobject. 我的钥匙应该是Gameobject。
What I have so far: 到目前为止,我有:
foreach(GameObject plane in anchorManager.planesSpawned)
{
if (ContainsKeyValue(planesFilled.ToDictionary(), plane)) { //dont know value
} else {
planesFilled.Add (new KeyValuePair<GameObject, float>(plane, GetDistanceBetweenObjs(plane.transform, botController.transform)));
}
//planesFilled[plane] = new KeyValuePair<GameObject, float>(plane, GetDistanceBetweenObjs(plane.transform, botController.transform));
//list.Add(new KeyValuePair<double,double>(p.Key,p.Value));
planesFilled.Sort((pair1,pair2) => pair1.Value.CompareTo(pair2.Value));
}
This func is started but doesnt exactly work in my scenario: 此功能已启动,但在我的情况下无法正常工作:
public bool ContainsKeyValue<TKey, TVal>(Dictionary<TKey, TVal> dictionnaire,
TKey expectedKey,
TVal expectedValue) where TVal : IComparable
{
TVal actualValue;
if (!dictionnaire.TryGetValue(expectedKey, out actualValue))
{
return false;
}
return actualValue.CompareTo(expectedValue) == 0;
}
How can I update the values of existing objects or add new ones to my list dictionary situation? 如何更新现有对象的值或将新对象添加到列表字典的情况?
How can I update the values of existing objects or add new ones to my list dictionary situation? 如何更新现有对象的值或将新对象添加到列表字典的情况?
I can't answer about your "list dictionary"; 关于您的“列表字典”,我无法回答; and I'm not sure what is making you hesitant about using an actual Dictionary . 而且我不确定是什么使您对使用实际的词典犹豫不决。 But if you did, you could do: 但是,如果这样做,则可以执行以下操作:
yourDictionary["yourKey"] = yourData;
Which will add or upate a dictionary pair. 这将添加或更新字典对。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.