Does Array.Copy work with multidimensional arrays?

后端 未结 4 916
被撕碎了的回忆
被撕碎了的回忆 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(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(ref arrayOrigin, vNumRows, vNumCols);
    

提交回复
热议问题