开发者

Adding to an Array

开发者 https://www.devze.com 2022-12-29 23:26 出处:网络
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开发者_如何学Python be don

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开发者_如何学Python be done? I keep getting a message saying that the String cannot be converted to String[].

Thank you


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();


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.


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


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.


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.


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");


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; 

}


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

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号