Does Array.Copy work with multidimensional arrays?

后端 未结 4 905
被撕碎了的回忆
被撕碎了的回忆 2020-12-08 20:30

This code works fine:

var newArray = new Rectangle[newHeight, newWidth];

for (int x = 0; x < newWidth; x++)
    for (int y = 0; y < newHeight; y++)
           


        
相关标签:
4条回答
  • 2020-12-08 21:03

    I use this code:

    public static void ResizeBidimArrayWithElements<T>(ref T[,] original, int rows, int cols)
    {
    
        T[,] newArray = new T[rows, cols];
        int minX = Math.Min(original.GetLength(0), newArray.GetLength(0));
        int minY = Math.Min(original.GetLength(1), newArray.GetLength(1));
    
        for (int i = 0; i < minX; ++i)
            Array.Copy(original, i * original.GetLength(1), newArray, i * newArray.GetLength(1), minY);
    
        original = newArray;
    
    }
    

    calling like this for array of strings

    ResizeBidimArrayWithElements<string>(ref arrayOrigin, vNumRows, vNumCols);
    
    0 讨论(0)
  • 2020-12-08 21:09

    I had a need to consume data from a buffer and copy off to a large holding array before the next interrupt hit. Copying in a loop wasn't an option; far too slow. I didn't need the multidimensional structure of the combined data until all of the copying was done, this meant I could Buffer.BlockCopy() to a single dimension array, then copy again onto a multidimensional array to obtain the required structure. Here's some code (run it in a console) that will demonstrate the technique as well as the performance.

    static class Program
    {
        [STAThread]
        static void Main()
        {
            Stopwatch watch = new Stopwatch();
    
            const int width = 2;
            const int depth = 10 * 1000000;
    
            //  Create a large array of data
            Random r = new Random(100);
            int[,] data = new int[width, depth];
            for(int i = 0; i < width; i++)
            {
                for(int j = 0; j < depth; j++)
                {
                    data[i, j] = r.Next();
                }
            }
    
            //  Block copy to a single dimension array
            watch.Start();
            int[] buffer = new int[width * depth];
            Buffer.BlockCopy(data, 0, buffer, 0, data.Length * sizeof(int));
            watch.Stop();
            Console.WriteLine("BlockCopy to flat array took {0}", watch.ElapsedMilliseconds);
    
            //  Block copy to multidimensional array
            int[,] data2 = new int[width, depth];
            watch.Start();
            Buffer.BlockCopy(buffer, 0, data2, 0,buffer.Length * sizeof(int));
            watch.Stop();
            Console.WriteLine("BlockCopy to 2 dimensional array took {0}", watch.ElapsedMilliseconds);
    
    
            //  Now try a loop based copy - eck!
            data2 = new int[width, depth];
            watch.Start();
            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < depth; j++)
                {
                    data2[i, j] = data[i, j];
                }
            }
            watch.Stop();
            Console.WriteLine("Loop-copy to 2 dimensional array took {0} ms", watch.ElapsedMilliseconds);
        }
    }
    

    Output:

    BlockCopy to flat array took 14 ms
    BlockCopy to 2 dimensional array took 28 ms
    Loop-copy to 2 dimensional array took 149 ms
    
    0 讨论(0)
  • 2020-12-08 21:13

    Yes. However, it doesn't work the way you are thinking it works. Rather, it thinks of each mutlidimensional array as a single-dimensional array (which is actually what they are in memory, it's just a trick that lets us place some structure on top of them to think of them as multidimensional) and then copies the single-dimensional structures. So if you have

    1 2 3
    4 5 6
    

    and want to copy it into

    x x x x
    x x x x
    

    then it will think of the first array as

    1 2 3 4 5 6
    

    and the second as

    x x x x x x x x
    

    and the result will be

    1 2 3 4 5 6 x x
    

    which will appear to you as

    1 2 3 4
    5 6 x x
    

    Got it?

    0 讨论(0)
  • 2020-12-08 21:13

    Simple use the "Clone()" function like the following:

    This is your array list

    object newArray = new object [row, column];
    

    When you are creating another Array just use this code:

    object[,] clonedArray = (object[,]) newArray.Clone();
    

    Simple! Have fun!

    0 讨论(0)
提交回复
热议问题