Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resize multidimensional (2D) array in C#?

Tags:

arrays

c#

I tried the following but it just returns a screwed up array.

    T[,] ResizeArray<T>(T[,] original, int rows, int cols)     {         var newArray = new T[rows,cols];         Array.Copy(original, newArray, original.Length);         return newArray;     } 
like image 744
Manuel Avatar asked Jun 30 '11 18:06

Manuel


People also ask

Can you change the size of a 2D array?

Similar to the one-dimensional array, the length of the two-dimensional array is also fixed. You can not change the length of an array, I mean, the number of rows and columns will remain fixed.

How do you determine the size of a multidimensional array?

Size of multidimensional arrays: The total number of elements that can be stored in a multidimensional array can be calculated by multiplying the size of all the dimensions. For example: The array int[][] x = new int[10][20] can store a total of (10*20) = 200 elements.

Can C language handle multidimensional arrays?

In C programming, you can create an array of arrays. These arrays are known as multidimensional arrays.

What is the maximum size of 2D array in C?

If the array is declared using automatic storage duration, then the size limit is remarkably small, at around 1Mb. If you're using dynamic storage duration (using new and new[] ), then then limit is much higher.


1 Answers

Most methods in the array class only work with one-dimensional arrays, so you have to perform the copy manually:

T[,] ResizeArray<T>(T[,] original, int rows, int cols) {     var newArray = new T[rows,cols];     int minRows = Math.Min(rows, original.GetLength(0));     int minCols = Math.Min(cols, original.GetLength(1));     for(int i = 0; i < minRows; i++)         for(int j = 0; j < minCols; j++)            newArray[i, j] = original[i, j];     return newArray; } 

To understand why it doesn't work with Array.Copy, you need to consider the layout of a multidimensional array in memory. The array items are not really stored as a bidimensional array, they're stored contiguously, row after row. So this array:

{ { 1, 2, 3 },   { 4, 5, 6 } } 

Is actually arranged in memory like that: { 1, 2, 3, 4, 5, 6 }

Now, assume you want to add one more row and one more column, so that the array looks like this:

{ { 1, 2, 3, 0 },   { 4, 5, 6, 0 },   { 0, 0, 0, 0 } } 

The layout in memory would now be as follows: { 1, 2, 3, 0, 4, 5, 6, 0, 0, 0, 0, 0 }

But Array.Copy treats all arrays as one-dimensional. MSDN says:

When copying between multidimensional arrays, the array behaves like a long one-dimensional array, where the rows (or columns) are conceptually laid end to end

So when you try to copy the original array to the new one, it just copies one memory location to the other, which gives, in one-dimensional representation:

{ 1, 2, 3, 4, 5, 6, 0, 0, 0, 0, 0, 0 }.

If you convert that to a two-dimensional representation, you get the following:

{ { 1, 2, 3, 4 },   { 5, 6, 0, 0 },   { 0, 0, 0, 0 } } 

This is why you're getting a screwed up array... Note that it would work property if you changed the number of rows, but not the number of columns.

like image 166
Thomas Levesque Avatar answered Sep 27 '22 20:09

Thomas Levesque