【问题标题】:How to set a list within an array如何在数组中设置列表
【发布时间】:2013-03-10 11:11:49
【问题描述】:

我发起了一个多维数组:

private byte[,] grid = new byte[9, 9];

我想为每个具有现有网格的单元格保存一个包含一些值的字节列表。

我知道 3 维数组在这里会很方便。但由于无效值的列表是动态的,我想使用列表而不是数组。

编辑:在这里给出一些上下文。我正在制作一个数独,它由一个多维数组表示。由于我想以编程方式解决数独问题并且我使用的是回溯算法,因此我需要记住每个单元格无效的数字。

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

【问题讨论】:

    标签: c# arrays list multidimensional-array


    【解决方案1】:
    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>();
    

    之后,9x9 数组的每一项都包含一个空的List&lt;byte&gt;

    但是,我建议创建一个像 Field 这样的类

    public class Field
    {
        public byte Value { get; set; }
        public List<byte> List { get; set; }
    
        public Field()
        {
            List = new List<byte>();
        }
    }
    

    并使用它来代替List&lt;byte&gt;

    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();
    

    【讨论】:

    • 请回复。似乎与 C Sharper 建议的相同。如果没有其他解决方案,我会尝试。因为它确实更接近我的喜好,所以是一个 3 维数组。
    • 哇哦,确实如此。我没想到>
    【解决方案2】:

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

      List<List<byte>> listOfBytesInList = new List<List<byte>>();
    

    更新

    您可以实现一个使用内部字节列表的自己的类。 我来晚了,但无论如何我都会发布我的解决方案。

     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;
    
            }
    

    【讨论】:

    • 没想到。但我仍然需要一个实际的领域。所以 1 个单元格应该由一个具有字节值的实际字段和一个字节类型的列表组成。
    猜你喜欢
    • 1970-01-01
    • 2016-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-04
    • 2023-03-18
    • 2017-07-19
    • 2023-03-06
    相关资源
    最近更新 更多