C# 基于字典输出数组数据
我目前正试图从我的程序中注销3条信息:C# 基于字典输出数组数据,c#,arrays,dictionary,C#,Arrays,Dictionary,我目前正试图从我的程序中注销3条信息: 阵列中的位置 与此位置关联的值 与此位置关联的字典键值 然而,我想我已经弄糊涂了该怎么做。请有人看一下我的代码,告诉我我做错了什么,因为它没有输出我所需的信息: float[] foo_array = new float[dictionary.Count]; dictionary.Values.CopyTo(foo_array, 0); Array.Sort(foo_array); foreach (string key in dictionary.K
float[] foo_array = new float[dictionary.Count];
dictionary.Values.CopyTo(foo_array, 0);
Array.Sort(foo_array);
foreach (string key in dictionary.Keys)
{
for( int i = 0; i < foo_array.Length; i++)
{
Debug.Log("Array position: " + i + " ARRAY VALUE : " + foo_array[i] + " key value: " + key);
}
}
float[]foo_数组=新的float[dictionary.Count];
dictionary.Values.CopyTo(foo_数组,0);
排序(foo_数组);
foreach(dictionary.Keys中的字符串键)
{
for(int i=0;i
我认为您所追求的是在字典中迭代查找它的位置、相关键和值。
您无需将密钥复制到数组即可:
var formatStrings =
dictionary.Select((kvp, index) =>
string.Format(
"Position: {0} Key: {1} Value: {2}", index, kvp.Key, kvp.Value));
foreach (var formatString in formatStrings)
{
Debug.Log(formatString);
}
你能详细说明“不输出我所需的信息”吗?它到底是在输出什么,还是输出的东西超出了你的预期?请具体说明。我将键值放在一个数组中,因为我需要发送3个最小的值。当它们的值随时间变化时,我需要数组不断自我更新,以便数组中的前三个位置是您可以执行的最小值
dictionary.OrderBy(=>uwy.Value)。选择(…)
,以保持排序。如果在复制值后对数组进行排序,那么索引位置将不再与字典相关。我不知道,谢谢,但是,我需要三个最小的值来插入其他内容,我认为最简单的处理方法是插入数组位置,在这种情况下,您可能会执行var-toSend=dictionary.OrderBy(=>\uvalue).Take(3)代码>这回答了你的问题吗?如果是,请接受。谢谢。另外,如果你需要经常整理字典,我建议你看看。