C# Splitting An Array

后端 未结 9 1340
情深已故
情深已故 2020-12-10 00:39

I need to split an array of indeterminate size, at the midpoint, into two separate arrays.

The array is generated from a list of strings using ToArray().

<         


        
9条回答
  •  感情败类
    2020-12-10 01:31

    Why are you passing the UList as ref? There doesn't appear to be a need for that.

    I would use a generic Split method if I needed to do this:

    public void Split(T[] array, out T[] left, out T[] right)
    {
        left = new T[array.Length / 2];
        right = new T[array.Length - left.Length];
    
        Array.Copy(array, left, left.Length);
        Array.Copy(array, left.Length, right, 0, right.Length);
    }
    

提交回复
热议问题