Insert one dimensional array into multidimensional array

时光怂恿深爱的人放手 提交于 2020-01-06 02:50:23

问题


I'm looking for a way to input 4 one-dimensional arrays into a 4x4 multidimensional array.

In the time I've spent looking for this I've found that it seems much simpler to do with jagged arrays. However, I feel like I'm missing something obvious and would like to ask for help.

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

this is how I've been able to do it with the jagged array which is defined earlier as:

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

Is there code similar to this that would allow me to assign values by line to a multidimensional array without having to do 16 passes to assign individual elements?


回答1:


I think you're looking for Buffer.BlockCopy()

For more details, see Documentation of BufferCopy.BlockCopy




回答2:


for scalability you can try custom collections like this

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

then your loop will be looked like that:

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


来源:https://stackoverflow.com/questions/16095751/insert-one-dimensional-array-into-multidimensional-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!