Solved

# two-dimensional dynamic array in C#

Posted on 2001-07-03
830 Views
Does anyone know if it's possible to create an multidimensional array (i only need two dimensions) in C#, such that it's possible to extend the lengt of the dimesions.

I do not need a "jagged array" that can have different length on the arrays in the second dimension, a perfectly square layout is acceptable.

As far as I've seen it's possible to make ArrayLists which are dynamic but one-dimensional or multidimensional arrays wich have static length.

Thanks for any help
0
Question by:micja491

LVL 22

Expert Comment

ID: 6249360
I don't really understand your question, but the following shows how you can create a multi-dimensional array in C#

int[,] myarray = {{1,2},{3,4},{6,7}};

Of course there are other ways of accomplishing the same thing. However, you will need to watch for one thing. All lengths of the array need to be the same length. So the above is equivalent to:
int[] myarray = new int[3,2];
myarray[0,0] = 1;
myarray[0,1] = 2;
myarray[1,0] = 3
...etc...

regards,
CJ
0

Author Comment

ID: 6249417
Well.. that's exactly what i want. A two-dimensional array with all the lengths the same.

But I need to be able to extend the array from it's initial length. So if I declare it as an int[3,2] then later on I might need to add elements to it so it's really an int[4,20].

Basically the fuction of a redim in VB. That's what I mean by dynamic that the dimensions of the array can grow.

Is that possible with C# arrays?
0

LVL 5

Expert Comment

ID: 6249695
Hi,

Is this what you need:

{
int L1,L2,i;
int **twoD;
L1 = 2; L2 = 4;
NewArray(twoD, 2, 4);
twoD[0][3] = 111;
DelArray(twoD, 2);
NewArray(twoD, 3, 6);
DelArray(twoD, 3);
}

void NewArray(int **& twoD, int L1, int L2)
{     twoD = new int*[L1];
for (int i=0; i<L1; i++)
{     twoD[i] = new int[L2];
}
}

void DelArray(int **& twoD, int L1)
{     for (int i=0; i<L1; i++)
{     delete [] twoD[i];
}
delete [] twoD;
}
0

LVL 22

Expert Comment

ID: 6250224
No, unfortunately there is no pre-defined function or parameter that you can use to add another element. You will have to copy all elements to another array. You can however use unsafe code and use the code that jklmn showed. Though a couple of changes would have to be made :-/

At least the function is still not available in Beta 2 :-(

Regards,
CJ

0

LVL 2

Expert Comment

ID: 6251222
You can just keep a linked list, where each node is ANOTHER linked list containing the columns... OR you can just use the standard template library (vectors, or something similar).
0

Author Comment

ID: 6251662
Yeah I could create a linked list but the thing is my Idea was to use the structure with a grids BindToArray-method and I doubt that it will work if it's not a standard array-type.

Anyway, what is the standard template library?
0

LVL 22

Expert Comment

ID: 6251704
in C++ you can create a vector-list and add new items to it each and every time. However, every time an item is added no reallocation of memory is done. It just copies the current contents and creates a new list.

regards,
CJ
0

Author Comment

ID: 6251777
Thanks everyone... what I wanted was something like Delphis
multidimensional dynamic arrays, but I guess there's no such luck.

Here's how it's done in Delphi.
======================================================
To declare multidimensional dynamic arrays, use iterated array of ... constructions. For example,

type TMessageGrid = array of array of string;
var Msgs: TMessageGrid;

declares a two-dimensional array of strings. To instantiate this array, call SetLength with two integer arguments. For example, if I and J are integer-valued variables,

SetLength(Msgs,I,J);

allocates an I-by-J array, and Msgs[0,0] denotes an element of that array.
You can create multidimensional dynamic arrays that are not rectangular. The first step is to call SetLength, passing it parameters for the first n dimensions of the array. For example,

var Ints: array of array of Integer;
SetLength(Ints,10);

allocates ten rows for Ints but no columns. Later, you can allocate the columns one at a time (giving them different lengths); for example

SetLength(Ints[2], 5);

makes the third column of Ints five integers long. At this point (even if the other columns haven?t been allocated) you can assign values to the third column?for example, Ints[2,4] := 6.

If anyone knows an easy way to do this in C# I'll be happy otherwise I'll have to think of some other way to solve my problem.
0

LVL 22

Accepted Solution

CJ_S earned 105 total points
ID: 6251885
Microsoft has no information about redimensioning arrays in C#, while they do for the new VB.Net platform (is the same as in previous versions though). I'm surprised that redimensioning is not an option in C#....

You can use the Array.Copy method to copy all contents of the array to another array...of course you should declare a new entry too.

"Except for the stackalloc operator, C# provides no predefined constructs for managing non-garbage collected memory. Such services are typically provided by supporting class libraries or imported directly from the underlying operating system. For example, the Memory class below illustrates how the Heap Functions of the Windows API can be accessed from C#:"

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncsspec/html/vclrfcsharpspec_a_8.asp

Regards,
CJ
0

LVL 22

Expert Comment

ID: 6251891
*grin* it's good to see that the article also mentions the same way as I myself have been thinking of :-)

Regards,
CJ
0

Author Comment

ID: 6251912

I only whish I could work with Delphi instead ;)
0

LVL 22

Expert Comment

ID: 6251936
Glad to be of service. Why did you grade this as a B though...just wanna know.

regards,
CJ
0

Author Comment

ID: 6251960
Well I guess I graded it after to what degree the answer solved my problem and not after the efort you put into helping me.

Your efforts do deserve an A, but the solution is more good than excelent.

Maybe I didn't grade according to the right criteria?
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may heâ€¦
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in aâ€¦
Starting up a Project