C# Splitting An Array

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

    Why don't you allocate two arrays and copy the contents ?

    EDIT: here you go:

            String[] origin = new String[4];
            origin[0] = "zero";
            origin[1] = "one";
            origin[2] = "two";
            origin[3] = "three";
    
            Int32 topSize = origin.Length / 2;
            Int32 bottomSize = origin.Length - topSize;
            String[] sTop = new String[topSize];
            String[] sBottom = new String[bottomSize];
            Array.Copy(origin, sTop, topSize);
            Array.Copy(origin, topSize , sBottom, 0, bottomSize);
    

提交回复
热议问题