• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 894
  • Last Modified:

two-dimensional dynamic array in C#

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
micja491
Asked:
micja491
1 Solution
 
CJ_SCommented:
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
 
micja491Author Commented:
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
 
jklmnCommented:
Hi,

Is this what you need:

void CAboutDlg::OnButton1()
{
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
CJ_SCommented:
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
 
smitty1276Commented:
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
 
micja491Author Commented:
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
 
CJ_SCommented:
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
 
micja491Author Commented:
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
 
CJ_SCommented:
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.

Here's a link to an article which talkjs (kind of) about this:
"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#:"

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

Regards,
CJ
0
 
CJ_SCommented:
*grin* it's good to see that the article also mentions the same way as I myself have been thinking of :-)

Regards,
CJ
0
 
micja491Author Commented:
Thanks, you've been very helpful.

I only whish I could work with Delphi instead ;)
0
 
CJ_SCommented:
Glad to be of service. Why did you grade this as a B though...just wanna know.

regards,
CJ
0
 
micja491Author Commented:
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now