addinng a new element to the array of strings

  • Thread starter Thread starter Anatol Ciolac
  • Start date Start date
Anatol Ciolac said:
How dinamically add a new element to Array????

You can't - arrays are fixed length. What you want is an ArrayList.
 
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
 
Harris said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top