1

我正在寻找一种将 4 个一维数组输入 4x4 多维数组的方法。

在我花时间寻找这个的时候,我发现使用锯齿状数组似乎要简单得多。但是,我觉得我遗漏了一些明显的东西,想寻求帮助。

for (int x = 0; x <= 3; x++)
{
    //reads in 4 separate values e.g. A B C D
    unitReader = sr.ReadLine();  

    //creates a char array with 4 separate elements
    char[] line = unitReader.ToCharArray();

    //places that array into a bigger jagged array
    fullArray[x] = line; 

    //just to test that it's worked
    Console.WriteLine(fullArray[x]);
}

这就是我如何使用之前定义为的锯齿状数组来做到这一点:

char[][] fullArray = new char[4][];

是否有与此类似的代码允许我按行将值分配给多维数组,而无需执行 16 次传递来分配单个元素?

4

2 回答 2

0

为了可扩展性,您可以尝试这样的自定义集合

public class FixedSizeCollection<T> : Collection<T>
{
    protected bool _initializing;
    public int Size { get; private set; }

    public FixedSizeCollection(int size)
    {
        Size = size;
        Init();
    }

    public FixedSizeCollection(int size, IList<T> list) 
    {
        Size = size;
        Init();
        if (list.Count != Size)
            throw new InvalidOperationException("Changing size is not supported.");

        foreach (T item in list)
            Items[list.IndexOf(item)] = item;
    }

    protected virtual void Init()
    {
        _initializing = true;
        base.ClearItems();
        for (int j = 0; j < Size; j++)
            Add(default(T));
        _initializing = false;
    }

    protected override void ClearItems()
    {
        Init();
    }

    protected override void InsertItem(int index, T item)
    {
        if (!_initializing)
            throw new InvalidOperationException("Changing size is not supported.");
        base.InsertItem(index, item);
    }

    protected override void RemoveItem(int index)
    {
        if (!_initializing)
            throw new InvalidOperationException("Changing size is not supported.");
        base.RemoveItem(index);
    }

    protected override void SetItem(int index, T item)
    {
        base.SetItem(index, item);
    }
}

public class SquareArray<T> : FixedSizeCollection<FixedSizeCollection<T>>
{
    public SquareArray(int size) : base(size)
    {
    }

    protected override void Init()
    {
        _initializing = true;
        for (int i = 0; i< Size; i++)
        {
            FixedSizeCollection<T> row = new FixedSizeCollection<T>(Size);
            Add(row);
        }
        _initializing = false;
    }

    protected override void SetItem(int index, FixedSizeCollection<T> item)
    {
        if (item.Count != Size)
            throw new InvalidOperationException("Changing size is not supported.");
        base.SetItem(index, item);
    }
}

那么您的循环将如下所示:

SquareArray<char> fullarray = new SquareArray(4);

for (int x = 0; x <= 3; x++)
{
    //reads in 4 separate values e.g. A B C D
    unitReader = sr.ReadLine();  

    //creates a char array with 4 separate elements
    char[] line = unitReader.ToCharArray();

    //places that array into a bigger jagged array
    fullArray[x] = new FixedSizeCollection(4, line); 

    //just to test that it's worked
    Console.WriteLine(fullArray[x]);
}
于 2013-04-19T02:48:33.147 回答
0

我认为您正在寻找Buffer.BlockCopy()

有关更多详细信息,请参阅BufferCopy.BlockCopy 文档

于 2013-04-19T02:32:03.917 回答