C# Splitting An Array

后端 未结 9 1339
情深已故
情深已故 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:38

    If you don't have Linq, you can use Array.Copy:

    public void Split(ref UList list)
    {
        string[] s = list.mylist.ToArray();
    
        //split the array into top and bottom halfs
        string[] top = new string[s.Length / 2];
        string[] bottom = new string[s.Length - s.Length / 2];
        Array.Copy(s, top, top.Length);
        Array.Copy(s, top.Length, bottom, 0, bottom.Length);
    
        Console.WriteLine("Top: ");
        foreach (string item in top) Console.WriteLine(item);
        Console.WriteLine("Bottom: ");
        foreach (string item in bottom) Console.WriteLine(item);
    }
    

提交回复
热议问题