Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

two-dimensional dynamic array in C#

Posted on 2001-07-03
13
Medium Priority
?
879 Views
Last Modified: 2012-06-27
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
Comment
Question by:micja491
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
13 Comments
 
LVL 22

Expert Comment

by:CJ_S
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

by:micja491
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

by:jklmn
ID: 6249695
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 22

Expert Comment

by:CJ_S
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

by:smitty1276
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

by:micja491
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

by:CJ_S
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

by:micja491
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

by:
CJ_S earned 315 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.

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
 
LVL 22

Expert Comment

by:CJ_S
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

by:micja491
ID: 6251912
Thanks, you've been very helpful.

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

Expert Comment

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

regards,
CJ
0
 

Author Comment

by:micja491
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question