假设我有一个包含n个维度的.NET数组。我想通过这些元素进行预测并打印出类似的内容:
[0, 0, 0] = 2
[0, 0, 1] = 32
等等。我可以用一些Rank和dimension函数编写一个循环来得出索引。是否有内置函数?
答案 0 :(得分:2)
感谢您的回答,这是我在等待时写的内容:
public static string Format(Array array)
{
var builder = new StringBuilder();
builder.AppendLine("Count: " + array.Length);
var counter = 0;
var dimensions = new List<int>();
for (int i = 0; i < array.Rank; i++)
{
dimensions.Add(array.GetUpperBound(i) + 1);
}
foreach (var current in array)
{
var index = "";
var remainder = counter;
foreach (var bound in dimensions)
{
index = remainder % bound + ", " + index;
remainder = remainder / bound;
}
index = index.Substring(0, index.Length - 2);
builder.AppendLine(" [" + index + "] " + current);
counter++;
}
return builder.ToString();
}
答案 1 :(得分:1)
Take a look at this:可能对您有所帮助。