Dictionary<double, Tuple<int,int>> dictionary = new Dictionary<double, Tuple<int,int>>();
for (int i = 0; i < x.Length; i++)
for (int j = i+1; j < x.Length; j++)
{
double weight = Math.Round(Math.Sqrt(Math.Pow((x[i] - x[j]), 2) + Math.Pow((y[i] - y[j]), 2)), 2);
string edges = i + "-" + j + "-" + weight;
listBox1.Items.Add(edges);
Tuple<int, int> tuple = new Tuple<int, int>(i, j);
dictionary.Add(weight, tuple);
}
var list = dictionary.Keys.ToList();
list.Sort();
foreach (var key in list)
{
string a = dictionary[key].Item1 + "--" + dictionary[key].Item2 + "--> " + key.ToString();
listBox2.Items.Add(a);
}
我想在字典中存储一些值。但是在for循环中突然出现了与未完成值相冲突的情况。没有错误消息。 当我注释掉“dictionary.Add(weight,tuple);” listbox显示我想要的所有数据。c#字典自动打破for循环,
什么是'究竟x'? –
它是x-y坐标系统的数组,它的所有x个点的x值 – MuratAy
当你注释掉'dictionary.Add(weight,tuple);'时,列表框怎么可能显示出你想要的所有数据?列表框从字典中填充,如果您发表该评论,该列表将为空。 – Rob