1

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)]
7
  • Define "better" Commented May 23, 2022 at 14:18
  • I meant a less complex way. Ideally, without looping through the arrays; the way that my pseudo code is written. Commented May 23, 2022 at 14:22
  • Why is this "complex? Commented May 23, 2022 at 14:22
  • I believe that it is complex because I came out with a simpler way to write it. So if I came out with it there probably is some method or other mechanism in the language that is covering that. Commented May 23, 2022 at 14:29
  • Where did you use that simpler implementation? Why dont you share that here? Commented May 23, 2022 at 14:36

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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, y + indices.y] = subarray[x, y];
        }
    }
    return true;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.