Adding to an array

I have an array:

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

      

... But now I want to add to this array later, but I see no way to do it. How can this be done? I keep getting the message that the string cannot be converted to String [].

thanks

+2


a source to share


8 answers


Arrays cannot be resized after they are declared. Use collections instead. For example: List

.



+2


a source


Use a list, not an array:

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

      



Then, if you really need it as an array:

string[] ay = list.ToArray();

      

+6


a source


Arrays are of fixed size, so once you create it, you cannot resize it (without creating a new array object).

Use List<string>

instead of an array.

+3


a source


As everyone said, use List in the System.Collections.Generic namespace. You can also use Hashtable, which will allow you to give each row a value or "key", which gives you an easy way to pull out a specific row with a keyword. (as for storing messages stored in memory for whatever purpose). You can also create a new array each time you add a value, make the new array 1 larger than the old one, copy all data from the first array to the second array, and then add the new value to the last slot (length - 1) Then replace the old array with the new one. This is the most manual way to do it. But List and Hashtable work great too.

0


a source


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

0


a source


If you need indexing take a look at the data type of the dictionary also in System.Collection

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

so you can do something like

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

      

0


a source


Here's an extension method used to add an array to an array and create a new array of strings

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

      list.AddRange(arrayToAdd);   


      return list.ToArray();
    }
}

      

0


a source


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

// 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; 

      

}

0


a source







All Articles