通过 for 循环在内部使用 Lists<int> 初始化锯齿状数组
Initializing jagged arrays with Lists<int> inside by a for loop
当我想向锯齿状数组中的元素添加项目时,出现 NullReferenceException。
public List<int>[][] Map;
void Start()
{
Map = new List<int>[60][];
for(byte x = 0; x < 60 ; x++)
{
Map[x] = new List<int>[60];
// initialization of rows
}
Map [23] [34].Add (21);
}
你有一个锯齿状数组,它的每个元素都是一个 List<int>
。您初始化数组而不是元素。
因此,当您在未初始化的元素 List<int>
上调用 Add
时,您会得到异常。
Map = new List<int>[60][];
for (int x = 0; x < 60; x++)
{
Map[x] = new List<int>[60];
for (int y = 0; y < 60; y++)
{
Map[x][y] = new List<int>(); // initializing elements
}
// initialization of rows
}
Map[23][34].Add(21);
当我想向锯齿状数组中的元素添加项目时,出现 NullReferenceException。
public List<int>[][] Map;
void Start()
{
Map = new List<int>[60][];
for(byte x = 0; x < 60 ; x++)
{
Map[x] = new List<int>[60];
// initialization of rows
}
Map [23] [34].Add (21);
}
你有一个锯齿状数组,它的每个元素都是一个 List<int>
。您初始化数组而不是元素。
因此,当您在未初始化的元素 List<int>
上调用 Add
时,您会得到异常。
Map = new List<int>[60][];
for (int x = 0; x < 60; x++)
{
Map[x] = new List<int>[60];
for (int y = 0; y < 60; y++)
{
Map[x][y] = new List<int>(); // initializing elements
}
// initialization of rows
}
Map[23][34].Add(21);