可调整大小的多维表格

我需要存储一个包含truefalse值字段的棋盘,然后还要能够检查这些字段的值。当然,我可以创建一个包含行和列的单独类,将每个单独的字段存储在一个列表中,然后检查是否有包含所需坐标的条目,但这不是最佳方式。

在 Lua 中,我过去是这样做的:

local storedTab = {}

local function setValue(row, column)
    if not storedTab[row] then storedTab[row] = {} end
    storedTab[row][column] = true
end

local function getValue(row, column)
    return storedTab[row] and storedTab[row][column]
end

因此,一旦建立了一项行条目,我只会向已经创建的表添加一些列条目。我认为这是最优的。

编辑:那么如何在C#中实现它? 编辑2:棋盘将在游戏过程中增长。并且将在您死后停止。因此,没有办法知道要初始化它的任何维度。

点赞
用户3785314
用户3785314

编辑

原问题没有提到数组预期会随着时间增长而扩大。数组 不会 增长。一旦创建,它的大小就是固定的。

不过,你可以创建一个新的数组大小,并将旧的值从另一个数组复制到它里面,但我不建议这样做,因为还有其他的解决方案。

你可以使用List<List<bool>>。这很棘手,但可以通过一个简单的包装来完成。

public class BoolArray
{
    List<List<bool>> storedTab;

    public BoolArray(int newRowSize = 1, int newColumnSize = 1, bool defaultValue = false)
    {
        storedTab = new List<List<bool>>();
        reSize(newRowSize, newColumnSize, defaultValue);
    }

    //默认增加5
    public void reSize(int newRowSize = 5, int newColumnSize = 5, bool defaultValue = false)
    {
        //修复当newRowSize为0时的问题
        if (newRowSize <= 0)
        {
            //对每行添加/增加列
            int allRow = storedTab.Count;

            for (int i = 0; i < allRow; i++)
            {

                for (int j = 0; j < newColumnSize; j++)
                {
                    storedTab[i].Add(defaultValue);
                }
            }
        }
        else
        {
            for (int i = 0; i < newRowSize; i++)
            {
                //调整行大小
                storedTab.Add(new List<bool>());

                //调整列大小
                for (int j = 0; j < newColumnSize; j++)
                {
                    storedTab[i].Add(defaultValue);
                }
            }
        }
    }

    public void setValue(int row, int column, bool value)
    {
        storedTab[row][column] = value;
    }

    public bool getValue(int row, int column)
    {
        return storedTab[row][column];
    }

    public int rowSize
    {
        get { return storedTab.Count; }
    }

    public int columSize
    {
        get
        {
            return storedTab[0].Count;
        }
    }

    public void clear()
    {
        for (int i = 0; i < rowSize; i++)
        {
            for (int j = 0; j < columSize; j++)
            {
                storedTab[j].Clear();
            }
        }
        storedTab.Clear();
    }
}

用法

BoolArray boolArray = new BoolArray(1, 1);

//设置值
boolArray.setValue(0, 0, true);

Debug.Log("更改前的行数: " + boolArray.rowSize);
Debug.Log("更改前的列数: " + boolArray.columSize);

//按4和6缩放大小(现在为5乘7)
boolArray.reSize(4, 6);

Debug.Log("更改后的行数: " + boolArray.rowSize);
Debug.Log("更改后的列数: " + boolArray.columSize);

//按10和20缩放大小(现在为15乘27)
boolArray.reSize(10, 20);

Debug.Log("更改后的行数: " + boolArray.rowSize);
Debug.Log("更改后的列数: " + boolArray.columSize);

//获取值
Debug.Log("值: " + boolArray.getValue(0, 0));

boolArray.clear();
2017-01-29 22:25:43
用户1172363
用户1172363

也许你可以使用 HashTable

Hashtable storedTab = new Hashtable();

//添加一个值
storedTab[row+"-"+column] = true;   //创建一个唯一的键值,比如"1-3"(代表第1行第3列)

//检查该行/列是否有值
if (storedTab.ContainsKey(row+"-"+column))
{
   //使用 storedTab [row+"-"+column] 执行某些操作
}
2017-02-02 17:08:00