【发布时间】:2021-02-22 02:39:23
【问题描述】:
大家好,这段代码中发生了这个奇怪的事情
public CoreManager[][] mng = new CoreManager[5][];
void Start()
{
for (int x = 0; x < 3; x++)
{
for (int y = 0; y < 3; y++)
{
mng[y] = new CoreManager[5];
mng[x][y] = new CoreManager();
mng[x][y].actual_words = new string[5];
mng[x][y].actual_words[y] = "test "+y;
Debug.Log(mng[x][y].actual_words[y]); //this code work fine and show the elements in the log
}
}
Debug.Log(mng[0][0].actual_words[0]);//this code gives an error
}
如您所见,如果我尝试打印它会给我一个错误 初始化后数组的值,尽管如果它在初始化时位于 for 循环内,它会毫无问题地打印它
我昨天在这上面花了 7 个小时 :D
【问题讨论】:
-
我强烈推荐关注this Microsoft debugger tutorial。
-
不清楚您对这段代码的期望是什么,因为您似乎使用
x和y来表示许多不同的东西。奇怪的是,您声明mng包含 5 个数组,然后您只使用了 3 个插槽。然后,您使用y来表示嵌套数组中的位置和actual_words数组中的位置。