Best way to “push” into C# array

后端 未结 12 2620
没有蜡笔的小新
没有蜡笔的小新 2021-02-13 14:25

Good day all

So I know that this is a fairly widely discussed issue, but I can\'t seem to find a definitive answer. I know that you can do what I am asking for using a L

相关标签:
12条回答
  • 2021-02-13 14:29

    There are couple of ways this can be done.

    First, is converting to list and then to array again:

    List<int> tmpList = intArry.ToList();
    tmpList.Add(anyInt);
    intArry = tmpList.ToArray();
    

    Now this is not recommended as you convert to list and back again to array. If you do not want to use a list, you can use the second way which is assigning values directly into the array:

    int[] terms = new int[400];
    for (int runs = 0; runs < 400; runs++)
    {
        terms[runs] = value;
    }
    

    This is the direct approach and if you do not want to tangle with lists and conversions, this is recommended for you.

    0 讨论(0)
  • 2021-02-13 14:31

    As said before, List provides functionality to add elements in a clean way, to do the same with arrays, you have to resize them to accomodate extra elements, see code below:

    int[] arr = new int[2];
    arr[0] = 1;
    arr[1] = 2;
    //without this line we'd get a exception
    Array.Resize(ref arr, 3);
    arr[2] = 3;
    

    Regarding your idea with loop:

    elements of an array are set to their default values when array is initialized. So your approach would be good, if you want to fill "blanks" in array holding reference types (which have default value null).

    But it wouldn't work with value types, as they are initialized with 0!

    0 讨论(0)
  • 2021-02-13 14:32

    I don't think there is another way other than assigning value to that particular index of that array.

    0 讨论(0)
  • 2021-02-13 14:33

    array.push is like List<T>.Add. .NET arrays are fixed-size so you can't actually add a new element. All you can do is create a new array that is one element larger than the original and then set that last element, e.g.

    Array.Resize(ref myArray, myArray.Length + 1);
    myArray[myArray.GetUpperBound(0)] = newValue;
    

    EDIT:

    I'm not sure that this answer actually applies given this edit to the question:

    The crux of the matter is that the element needs to be added into the first empty slot in an array, lie a Java push function would do.

    The code I provided effectively appends an element. If the aim is to set the first empty element then you could do this:

    int index = Array.IndexOf(myArray, null);
    
    if (index != -1)
    {
        myArray[index] = newValue;
    }
    

    EDIT:

    Here's an extension method that encapsulates that logic and returns the index at which the value was placed, or -1 if there was no empty element. Note that this method will work for value types too, treating an element with the default value for that type as empty.

    public static class ArrayExtensions
    {
        public static int Push<T>(this T[] source, T value)
        {
            var index = Array.IndexOf(source, default(T));
    
            if (index != -1)
            {
                source[index] = value;
            }
    
            return index;
        }
    }
    
    0 讨论(0)
  • 2021-02-13 14:35

    As per comment "That is not pushing to an array. It is merely assigning to it"

    If you looking for the best practice to assign value to array then its only way that you can assign value.

    Array[index]= value;
    

    there is only way to assign value when you do not want to use List.

    0 讨论(0)
  • 2021-02-13 14:45

    The is no array.push(newValue) in C#. You don't push to an Array in C#. What we use for this is a List<T>. What you may want to consider (for teaching purpose only) is the ArrayList (no generic and it is a IList, so ...).

    static void Main()
    {
        // Create an ArrayList and add 3 elements.
        ArrayList list = new ArrayList();
        list.Add("One"); // Add is your push
        list.Add("Two");
        list.Add("Three");
    }
    
    0 讨论(0)
提交回复
热议问题