views:

109

answers:

8
+1  Q: 

Adding to an Array

Hi All

I have an array:

String[] ay = {
     "blah",
     "blah number 2"
     "etc" };

... But now I want to add to this array at a later time, but I see no option to do so. How can this be done? I keep getting a message saying that the String cannot be converted to String[].

Thank you

+7  A: 

Use a List rather than an array:

List<string> list = new List<string>();
list.Add( "blah" ) ;

Then, later, if you really do need it as an array:

string[] ay = list.ToArray();
tim_yates
+3  A: 

Arrays can't change their size after they are declared. Use collections instead. For example: List.

Petar Minchev
+4  A: 

Arrays are of fixed size, so after it has been created, you can't change the size of it (without creating a new array object)

Use the List<string> instead of the array.

Arjan Einbu
A: 

As everyone's already said, use List in the System.Collections.Generic namespace. You could also use a Hashtable which will allow you to give each string a meaning, or "key" which gives you an easy way to pull out a certain string with a keyword. (as for keeping messages stored in memory space for whatever purpose.) You could also Create a new array each time you add a value, make the new array 1 bigger than the old one, copy all the data from the first array into the 2nd array, and then add your new value in the last slot (Length - 1) Then replace the old array with your new one. It's the most manual way of doing it. But List and Hashtable work perfectly well too.

Skintkingle
A: 

If you don't need indexing a specific array element (usage of brackets), but you want to be able to efficiently add or remove elements, you could use LinkedList.

Bogi
A: 

If you do need indexing have a look at Dictionary data type also in the System.Collection

http://msdn.microsoft.com/en-us/library/xfhwa508.aspx

so you could do something like

        Dictionary<int, string> dictionary = new Dictionary<int, string>();
        dictionary.Add(1, "afljsd");
Luke
A: 

Here's an extension method to add the to arrays together and create a new string array

public static class StringArrayExtension
{
    public static string[] (this string[] currentArray, string[] arrayToAdd)
    {
      List<String> list = new List<String>(currentArray);

      list.AddRange(arrayToAdd);   


      return list.ToArray();
    }
}
Kevin
A: 

You can do this but I don't recommend it:

// Reallocates an array with a new size, and copies the contents
// of the old array to the new array.
// Arguments:
//   oldArray  the old array, to be reallocated.
//   newSize   the new array size.
// Returns     A new array with the same contents.
public static System.Array ResizeArray (System.Array oldArray, int newSize) {
   int oldSize = oldArray.Length;
   System.Type elementType = oldArray.GetType().GetElementType();
   System.Array newArray = System.Array.CreateInstance(elementType,newSize);
   int preserveLength = System.Math.Min(oldSize,newSize);
   if (preserveLength > 0)
      System.Array.Copy (oldArray,newArray,preserveLength);
   return newArray; 

}

MUG4N
System.Array already has a static Resize method.
Robert Davis