By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,948 Members | 838 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,948 IT Pros & Developers. It's quick & easy.

addinng a new element to the array of strings

P: n/a
How dinamically add a new element to Array????
Nov 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Anatol Ciolac <ac*****@ufmoldova.com> wrote:
How dinamically add a new element to Array????


You can't - arrays are fixed length. What you want is an ArrayList.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 22 '05 #2

P: n/a
Anatol Ciolac <ac*****@ufmoldova.com> wrote:
How dinamically add a new element to Array????


You can't - arrays are fixed length. What you want is an ArrayList.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 22 '05 #3

P: n/a
You could always create some kind so static util method that could
handle this; something like...
public static string[] addStringElement( string[] oldArray, string
element )
{
String[] newArray = new String[ oldArray.Length + 1 ];
Array.Copy( oldArray, newArray, oldArray.Length );
newArray[ oldArray.Length ] = element;
return newArray;
}

This may not be as fast as the ArrayList, but would keep the memory
consumption tight...

~harris

"Anatol Ciolac" <ac*****@ufmoldova.com> wrote in message news:<eB*************@tk2msftngp13.phx.gbl>...
How dinamically add a new element to Array????

Nov 22 '05 #4

P: n/a
Harris <mh********@mail.com> wrote:
You could always create some kind so static util method that could
handle this; something like...

public static string[] addStringElement( string[] oldArray, string
element )
{
String[] newArray = new String[ oldArray.Length + 1 ];
Array.Copy( oldArray, newArray, oldArray.Length );
newArray[ oldArray.Length ] = element;
return newArray;
}

This may not be as fast as the ArrayList, but would keep the memory
consumption tight...


Well, it would keep the absolute memory consumption tight, but in
creating more arrays in the first place, it would cause a lot more
memory "churn". It would be far better to use ArrayList while expanding
and then call TrimToSize when everything had been added, if you're
really worried about the extra size.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.