限制控制台上的2D数组大小(C#)

时间:2018-06-05 08:45:37

标签: c# android unity3d

这是我的代码

public int[,] GetBigEyeRoad(int x)
{
   int[,] arrayBigEyeResult = new int[6, x];

   Array.Copy(arrayBigEyeRoad, arrayBigEyeResult, arrayBigEyeRoad.GetLength(0) * arrayBigEyeRoad.GetLength(1));

   return arrayBigEyeResult;
}

并在我的主要课堂上调用它

int[,] arrayBigEyeRoad = bsb.GetBigEyeRoad(104);

    string s = "";

    for (int y = 0; y < arrayBigEyeRoad.GetLength(0); y++)
    {
        for (int x = 0; x < arrayBigEyeRoad.GetLength(1); x++)
        {
            s += string.Format("{0:D2}", arrayBigEyeRoad[y, x]);
            s += ".";
        }
        s += "\n";
    }
    Debug.Log(s);

这部分

int[,] arrayBigEyeRoad = bsb.GetBigEyeRoad(104);

我只想显示12二维数组值,就像这样

int[,] arrayBigEyeRoad = bsb.GetBigEyeRoad(12);

问题在于它不会让我。因为它会给我一个错误说

  

目标数组不够长。检查destIndex和length,以及数组的下限

现在我怎么可能做到这样呢

enter image description here

限制控制台上的2d显示

1 个答案:

答案 0 :(得分:3)

非常简单:

int[,] a1 = new int[100,200];
int[,] a2 = new int[10,5];

for (int i = 0; i < 10; i++)
    for (int j = 0; j < 5; j++)
        a2[i,j] = a1[i,j];

public class MyArray : int[,]
{
    public override string ToString()
    {
        string result = "";
        for (int i = 0; i < 10; i++)
            for (int j = 0; j < 5; j++)
                result += (a1[i,j].ToString() + ",");
        return result;
    }
}