2
假設我有一個有n個維數的.NET數組。我想通過元素進行foreach並打印出如下內容:.NET多維數組打印
[0, 0, 0] = 2
[0, 0, 1] = 32
依此類推。我可以用一些Rank和維度函數來編寫一個循環來提出索引。是否有內置功能?
假設我有一個有n個維數的.NET數組。我想通過元素進行foreach並打印出如下內容:.NET多維數組打印
[0, 0, 0] = 2
[0, 0, 1] = 32
依此類推。我可以用一些Rank和維度函數來編寫一個循環來提出索引。是否有內置功能?
感謝您的回答,這裏是我寫的,而我等着:
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();
}
Take a look at this:可能對您有所幫助。