0
如果我有一个数据结构定义如下:存储和访问2D数据
Dictionary<KeyValuePair<int, int>, object> rangedValue;
,我填充它是这样:
rangedValue = new Dictionary<KeyValuePair<int, int>, object>()
{
{ new KeyValuePair<int, int>(0,0), 424681 },
{ new KeyValuePair<int, int>(1,0), 1072301 },
{ new KeyValuePair<int, int>(2,0), 99111 },
{ new KeyValuePair<int, int>(3,0), 467874 },
{ new KeyValuePair<int, int>(0,1), 195066 },
{ new KeyValuePair<int, int>(1,1), 1171412 },
{ new KeyValuePair<int, int>(2,1), 0 },
{ new KeyValuePair<int, int>(3,1), 128504 }
}
,我想遍历它以协调的方式,如在(x,y)坐标值和检索值,在这一点上,我可以拿出最好的是这样的:
foreach (var relativeXOffset in rangedValue.Keys.Select(kv => kv.Key).Distinct()) // Note distinct here, otherwise returns duplicates
{
foreach (var relativeYOffset in rangedValue.Keys.Select(kv => kv.Value).Distinct())
{
var myObject = rangedValue[new KeyValuePair<int, int>(relativeXOffset, relativeYOffset)];
// Do something with myObject...
}
}
这个作品对我来说,但它似乎也有点垃圾。我的要求是将对象存储为一组坐标,然后能够以协调的方式遍历它们。任何人都可以帮助一个很好的解决方案,无论是在存储或检索方面(或者,理想情况下,两者)?
为什么不反对[] []? – Victor
http://codereview.stackexchange.com/不会更好吗? – Liam
@Liam - 好的电话 - 我会把它移到那边。 –