将列表的列表传输到2D数组 [英] Transferring a list of a list to a 2D array

查看:109
本文介绍了将列表的列表传输到2D数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好,所以我试图获取2D数组以在xml文件中序列化,然后能够加载它.主要部分起作用,但是我发现(可能是错误的)您不能在内容管道读取器和写入器中使用2d数组.我已将2d数组替换为List<List<string>>,但仍需要将数据作为2d数组.到目前为止,这是我所拥有的,但是它抛出了空引用异常:

Ok, so i'm trying to get a 2D array to serialize in an xml file and then be able to load it. The main part works but I have found (possibly false) that you can't use a 2d array in the content pipeline readers and writers. I have replaced the 2d array with a List<List<string>> but I still need the data as a 2d array. This is what I have so far but it throws an null reference exception:

TILArray = new string[Width, Height];// I do initialize the array

for (int x = 0; x < Width; x++)
{
    for (int y = 0; y < Height; y++)
    {
        if (TILList[x][y] != null)
        {
            Tiles[x, y] = Content.Load<Tile>(TILList[x][y]);
            TILArray[x, y] = TILList[x][y];// This line throws the exception
        }
    }
}

如果我要分配给它,是否应该给我一个null引用异常? 另外,如果有人知道如何直接在内容管道读取器和写入器中使用2d数组,我将不胜感激

Should it be giving me a null reference exception if I'm trying to assign to it? Also if anyone knows how to use 2d arrays directly in the content pipeline reader and writer i'd appreciate it

例外情况:

An unhandled exception of type 'System.NullReferenceException' occurred in TileEngine.dll

Additional information: Object reference not set to an instance of an object.

好吧,我一直在测试它,似乎它只在循环中时才抛出异常.当我尝试在循环外设置TILArray [0,0]时,它工作正常,但在循环中不起作用,并引发异常.

Well I've kept testing it and it seems that it only throws the exception when it's in the loop. When I try to set TILArray[0,0] outside the loop it works fine, but in the loop it doesn't work and throws the exception.

哇,我刚刚发现,如果我将抛出异常的行移到该行上方:

Tiles[x, y] = Content.Load<Tile>(TILList[x][y]);

它说只在}所在的行中抛出了异常!

It says the exception was thrown in the line that contains only a }!

我做了一些测试,发现问题不在于复制阵列. 当我注释掉引发异常的行时,它仍然会引发异常! 有什么会导致游戏从根本没有代码的那一行抛出NullReference异常?

I've done some more tests and have found that THE PROBLEM IS NOT WITH COPYING TO THE ARRAY. when I comment out the line that threw the exception it still throws it! Is there anything that would cause the game to throw a NullReference exception from a line where there isn't even any code?

推荐答案

是否考虑过数组数组?您可以使用TILArray[][]而不是TILArray[,],就像列表一样,它是一个数组数组.序列化程序应该能够处理此问题.

Have considered an array of arrays? Instead of TILArray[,], you'd use TILArray[][] which, like your list, is an array of arrays. The serialiser should be able to handle this.

这篇关于将列表的列表传输到2D数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆