Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在数组中设置列表_C#_Arrays_List_Multidimensional Array - Fatal编程技术网

C# 如何在数组中设置列表

C# 如何在数组中设置列表,c#,arrays,list,multidimensional-array,C#,Arrays,List,Multidimensional Array,我启动了一个多维数组: private byte[,] grid = new byte[9, 9]; 我想为现有网格中的每个单元格保存一个包含一些值的字节列表 我知道三维阵列在这里很方便。但是由于具有无效值的列表是动态的,因此我希望使用列表而不是数组 编辑:在此处提供一些上下文。我在做一个数独游戏,它由一个多维数组表示。因为我想用编程的方式来解决数独问题,而且我使用的是回溯算法,所以我需要记住对于每个单元格无效的数字 所以每个单元格应该由一个值和一个列表组成。我可能只是将实际字段作为列表中的第

我启动了一个多维数组:

private byte[,] grid = new byte[9, 9];
我想为现有网格中的每个单元格保存一个包含一些值的字节列表

我知道三维阵列在这里很方便。但是由于具有无效值的列表是动态的,因此我希望使用列表而不是数组

编辑:在此处提供一些上下文。我在做一个数独游戏,它由一个多维数组表示。因为我想用编程的方式来解决数独问题,而且我使用的是回溯算法,所以我需要记住对于每个单元格无效的数字


所以每个单元格应该由一个值和一个列表组成。我可能只是将实际字段作为列表中的第一个值。但是永远不知道是否有一个真正干净的解决方案:)

如果我理解正确,您需要列表中的字节列表吗? 试试这个:

  List<List<byte>> listOfBytesInList = new List<List<byte>>();
listofBytes列表=新列表();
更新

您可以实现自己的类,该类使用内部字节列表。 我太晚了,但不管怎样,我还是发布了我的解决方案

 public class ByteFieldList
    {
        private readonly List<byte> _interalBytes;

        public ByteFieldList()
            : this(4, 0)
        {
        }

        public ByteFieldList(byte fieldValue)
            : this(4, fieldValue)
        {
        }


        public ByteFieldList(int capacity, byte fieldValue)
        {
            FieldValue = fieldValue;
            _interalBytes = new List<byte>(capacity);
        }


        public byte FieldValue { get; set; }

        public ByteFieldList Add(byte b)
        {
            _interalBytes.Add(b);
            return this;
        }

        public void AddRange(byte[] bytes)
        {
            foreach (var b in bytes)
                Add(b);
        }


        public ByteFieldList Remove(byte b)
        {
            _interalBytes.Remove(b);
            return this;
        }

        public byte this[int index]
        {
            get { return _interalBytes[index]; }
        }

        public byte[] GetAllInvalid()
        {
            return _interalBytes.ToArray();
        }   

    }


  public void Usage()
        {
            ByteFieldList byteFieldList = new ByteFieldList(5);

            byteFieldList.Add(5).Add(4).Add(8);
            byteFieldList.AddRange(new byte[] { 7, 89, 4, 32, 1 });

            var invalids = byteFieldList.GetAllInvalid(); // get 5, 4, 8, 7, 89, 4, 32, 1

            byteFieldList.FieldValue = 4;

        }
公共类ByteFieldList
{
私有只读列表_字节间;
公共ByteFieldList()
:这(4,0)
{
}
公共字节字段列表(字节字段值)
:此(4,字段值)
{
}
公共ByteFieldList(整数容量,字节字段值)
{
FieldValue=FieldValue;
_字节间=新列表(容量);
}
公共字节字段值{get;set;}
公共字节字段列表添加(字节b)
{
_加入(b);
归还这个;
}
公共void AddRange(字节[]字节)
{
foreach(变量b,字节)
添加(b);
}
公共字节字段列表删除(字节b)
{
_细胞间。除去(b);
归还这个;
}
公共字节此[int索引]
{
获取{返回_字节间[索引];}
}
公共字节[]GetAllInvalid()
{
返回_interalBytes.ToArray();
}   
}
公共用途()
{
ByteFieldList ByteFieldList=新的ByteFieldList(5);
按字段列表。添加(5)。添加(4)。添加(8);
AddRange(新字节[]{7,89,4,32,1});
var invalids=byteFieldList.GetAllInvalid();//获取5,4,8,7,89,4,32,1
byteFieldList.FieldValue=4;
}
并使用它,而不仅仅是
列表

var grid=新字段[9,9];
对于(int i=0;i<9;i++)
对于(int j=0;j<9;j++)
网格[i,j]=新字段();

没有想到这一点。但我仍然需要一个实际的领域。因此,1个单元格应该由一个具有字节值的实际字段和一个用于应答的byte.Ty类型列表组成。似乎和C夏普建议的一样。如果没有其他解决办法,我会试试。因为它确实更符合我的喜好,然后是一个三维数组。哦,是的。我没有想到那件事。<那太愚蠢了:好吧,先生
var grid = new List<byte>[9,9];

for (int i = 0; i < 9; i++)
    for (int j = 0; j < 9; j++)
        grid[i, j] = new List<byte>();
public class Field
{
    public byte Value { get; set; }
    public List<byte> List { get; set; }

    public Field()
    {
        List = new List<byte>();
    }
}
var grid = new Field[9, 9];

for (int i = 0; i < 9; i++)
    for (int j = 0; j < 9; j++)
        grid[i, j] = new Field();