I defined this multi dimensional array in C#:
int[,] values = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } };
Now the values are not longer in this form but in a Dictionary:
values2.Add("KeyA", new List<float> {1,4});
values2.Add("KeyB", new List<float> {2,5});
values2.Add("KeyC", new List<float> {3,6});
Now I’m trying to parse this dictionary in the two dimensional array again, but somehow there are problems:
List<float> outList = new List<float>();
values2.TryGetValue(values2.Keys.ElementAt(0) as string, out outList);
int[,] values = new int[outList.Count, values2.Keys.Count];
for (int i = 0; i < values2.Keys.Count; i++)
{
List<float> list = new List<float>();
values2.TryGetValue(values2.Keys.ElementAt(i), out list);
for (int j = 0; j < list.Count; j++)
{
values[i, j] = Convert.ToInt32(list.ElementAt(j));
}
}
This throws an InnerException: System.IndexOutOfRangeException. But why? I’m not able to convert the values properly.
Edit: I can be assumed that all the values in the dictionary have the same list length. I do check this somewhere else.
I think it’s as simple as getting either your lengths or your indexing the wrong way round. Here’s your array creation:
And you’re then setting
values[i, j]whereiis less thanmetrics.Keys.Countandjis less thanlist.Count.You could either switch the lengths round or set
values[j, i]instead. Given the original statement of the array, I suspect you want the latter approach.