将一维数组插入多维数组



我正在寻找一种将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次传递来分配单个元素?

我想你正在寻找Buffer.BlockCopy()

要了解更多详细信息,请参见BufferCopy文档。BlockCopy

对于可伸缩性,您可以尝试这样的自定义集合

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

相关内容

  • 没有找到相关文章

最新更新