尝试将字符串写入多维 int 映射。
1,1,1,1,1,1,1,1,1,1,1
1,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,1
1,1,1,1,1,1,1,1,1,1,1
是的,你几乎可以猜到那是为了什么。碰撞地图。但无论如何,此信息存储在一个文件中。现在我得到了文件,转换和等等等等。我将只显示代码并显示我遇到问题的位置,这样可能更容易:
// Convert a string to multi-dim int array,
// format: 1,1,1,1|1,0,0,1|1,1,1,1|
private int[,] ConvertToMapArray(String data)
{
// Split to iterate rows and get row count.
string[] rows = data.Split('|');
// Initialize return data.
int[,] ret;
// Set row count to 0, increment each loop.
int rc = 0;
foreach (string row in rows)
{
// Split each number and iterate. Convert to int32 and and store in its own int array.
string[] items = row.Split(',');
// Initialize int array with proper row count.
int[] newRow = new int[ items.Count() ];
// Count set to 0 for foreach iteration, must increment.
int ic = 0;
foreach (string item in items )
{
// Convert string to int.
int i int.Parse(item);
// Add to newRow int array.
newRow[ic++] = i;
}
// Add new int row to return multi-dim array.
ret[rc++] = newRow; /// <--- this doesnt work
}
return ret;
}