The*_*den 0 c# arrays reference list object
是否可以在数组中存储包含List的类?
我对这个概念有些麻烦.
这是我的代码:
我的类称为"arrayItems":
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace EngineTest
{
[Serializable] //List olarak save etmemiz için bu gerekli.
public class arrayItems
{
public List<items> items = new List<items>();
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的数组的定义叫做"tileItems":
public static arrayItems[, ,] tileItems;
Run Code Online (Sandbox Code Playgroud)
以下是我创建数组的方法:
Program.tileItems = new arrayItems[Program.newMapWidth, Program.newMapHeight, Program.newMapLayers];
Run Code Online (Sandbox Code Playgroud)
我面临的问题是我的Array的内容为null.我收到这个错误:
Object reference not set to an instance of an object.
当我尝试通过Add()命令在数组中填充List时,我得到了同样的错误.
你能指引我走向正确的方向吗?提前致谢.
您需要初始化数组中的每个列表:
for (int i = 0; i < newMapWidth; i++)
{
for (int j = 0; j < newMapHeight; j++)
{
for (int k = 0; k < newMapLayers; k++)
{
arrayItems[i,j,k] = new arrayItems();
}
}
}
Run Code Online (Sandbox Code Playgroud)
第一.
| 归档时间: |
|
| 查看次数: |
1170 次 |
| 最近记录: |