Home > OS >  C# How to paste 2D array[,] onto second 2D array[,] on specified indexes
C# How to paste 2D array[,] onto second 2D array[,] on specified indexes

Time:05-24

is there any better (less complex) way to paste the smaller array[,] to the bigger array[,] than looping through them? My code:

private void PasteRoomIntoTheMap(GameObject[,] room, (int, int) positionOnTheMap)
{
    (int, int) roomSize = (room.GetLength(0), room.GetLength(1));
    int roomsXAxesDimention = 0;
    int roomsYAxesDimention = 0;
    for (int i = positionOnTheMap.Item1; i <= positionOnTheMap.Item1   roomSize.Item1; i  )
    {
        for (int j = positionOnTheMap.Item2; j <= positionOnTheMap.Item2   roomSize.Item2; j  )
        {
            Map[i, j] = room[roomsXAxesDimention, roomsYAxesDimention];
            roomsYAxesDimention  ;
        }
        roomsXAxesDimention  ;
    }
}

(I didn't run it yet. There might be some errors but I hope that you will understand this method)

The code that I would love to have:

Map [5...15, 2...5] = room[0...room.GetLength(0), 0...room.GetLength(1)]

CodePudding user response:

Short answer: No

Longer answer:

Multidimensional arrays are really a wrapper around a 1D array, with some special code to handle indices etc. It does however lack some useful features.

You could perhaps improve your example code a bit by copying entire columns or rows using Array.Copy or blockCopy instead of processing item by item. This might help performance if you have very many items that need copying, but is unlikely to make any difference if the sizes are small.

You could probably provide the syntax you desire by creating something like a ArraySlice2D class that reference the original 2D array as well as the region you want to copy. But you probably need to either create your own wrapper to provide index operators, or create extension methods to do it. Perhaps something like

public class My2DArray<T>{
    private T[,] array;
public ArraySlice2D<T> this[Range x, Range y]{
   get => new ArraySlice2D<T>(array, x, y);
   set {
       // Copy values from value.RangeX/Y to x/y ranges
   }
}

You might also consider writing your own 2D array class that wraps a 1D array. I find that this often makes interoperability easier since most systems can handle 1D arrays, but few handle multidimensional arrays without conversion.

CodePudding user response:

I am not aware of any shortcut for that and there might be to many slightly distinct usecase so that the most apropriate solution would be to implement your own functionality.

It is advisable however to add a size check to your method so you dont end up indexing nonexisting areas of the big array:

private bool ReplaceWithSubArray(int[,] array, int[,] subarray, (int x,int y) indices)
{
    if (array.GetLength(0) < subarray.GetLength(0)   indices.x ||
                array.GetLength(1) < subarray.GetLength(1)   indices.y)
    {
        // 'array' to small
        return false;
    }
    for (int x = 0; x <= subarray.GetLength(0); x  )
    {
        for (int y = 0; y <= subarray.GetLength(1); y  )
        {
            array[x   indices.x, indices.y] = subarray[x, y];
        }
    }
    return true;
}
  • Related