Archived

This topic is now archived and is closed to further replies.

jim bob

Resizeable array C#

Recommended Posts

So I''m sitting here using C# like it''s nobody''s business but mine, right? My problem is as follows. I need to output a series of values; the type is arbitrary. But I do not know how many values I will need to output. I might output 10, I might output 3, I might output 78. Anything is possible, and I do not want to waste space by declaring an array of 300. I imagine this might fall into linked list territory? Is that right, or no? Anyway, what is a good solution to this basic problem. I essentially need a resizeable array. Ideas on implementation in C#? I am going for simplicity and ease of understanding, not anything unexplainable here, if possible. Thanks for any guidance.

Share this post


Link to post
Share on other sites
Look into System.Collections for a bunch of implemented collections in the .NET Framework. ArrayList is probably what you are currently looking for.

Magius

Share this post


Link to post
Share on other sites
ArrayList is a dynamically sizeable array with all the standard dynamic array functionality you''d expect.

Queue implements a queue where you stuff things on the end and pull things off the front.

Stack lets you stick things on the top and then pull them off in reverse order.

Hashtable is my favourite, and is your standard dictionary/map collection implementation.

Snowman | Ardaxus | Nathan
---------------------------------
Tangle - Persistent World Toolkit
tangle.netlab.com.au

Share this post


Link to post
Share on other sites
I''ve read your problem, and your definitly after an ArrayList. Basically you do this:

ArrayList myArray = new ArrayList();
string myData = "Hello";

myArray.add(myData)

MessageBox.show(myArray[0]);

// Loop though ArrayList

for(int x=0; x < myArray.count; x++)
{
// This will loop however many items you have.
}

Share this post


Link to post
Share on other sites
quote:
Original post by Manip
I''ve read your problem, and your definitly after an ArrayList. Basically you do this:

ArrayList myArray = new ArrayList();
string myData = "Hello";

myArray.add(myData)

MessageBox.show(myArray[0]);

// Loop though ArrayList

for(int x=0; x < myArray.count; x++)
{
// This will loop however many items you have.
}



Using a for() loop with myArray.count is ugly, use

foreach(string s in myArray)
{
// Do something with s

}


jim bob: you can create an array dynamically, with

// user inputs how many lines of output he wants in int mysize
string[] myarray = new string[mysize];
// Now fill myarray

However you cannot resize it afterwards, eg. double it in size or keep adding lines, like Delphi''s dynamic arrays. In that case just use an ArrayList.

Share this post


Link to post
Share on other sites
quote:
Original post by Fidelio66
Using a for() loop with myArray.count is ugly, use

It has been suggested that using a for() loop is preferable to foreach.

Joel Martinez
http://www.codecube.net/

[edited by - joelmartinez on April 21, 2004 11:52:42 AM]

Share this post


Link to post
Share on other sites
Here''s a tip -- keep the C# value-type box/unbox behavior in mind when you use these. If you put a value type (such as a struct) into the ArrayList then try to modify it later on, you''re not going to succeed without jumping though some major hoops. And the worst part is, it''s the sort of bug where the code compiles fine and runs without crashing, it just behaves oddly.

This is a good overview of the problem:

http://www.c-sharpcorner.com/Code/2003/July/BoxNUnboxSSF.asp

It sounds like a simple thing, and it is -- but when it crops up as an unexpected bug, you''ll pull your hair out until you remember this little issue. Ahem. Take my word for it.

Share this post


Link to post
Share on other sites