C# Splitting An Array

后端 未结 9 1322
情深已故
情深已故 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);
    
    0 讨论(0)
  • 2020-12-10 01:20

    Use a generic split method:

    public static void Split<T>(T[] source, int index, out T[] first, out T last)
    {
        int len2 = source.Length - index;
        first = new T[index];
        last = new T[len2];
        Array.Copy(source, 0, first, 0, index);
        Array.Copy(source, index, last, 0, len2);
    }
    
    0 讨论(0)
  • 2020-12-10 01:21

    I also want to add a solution to split an array into several smaller arrays containing a determined number of cells.

    A nice way would be to create a generic/extension method to split any array. This is mine:

    /// <summary>
    /// Splits an array into several smaller arrays.
    /// </summary>
    /// <typeparam name="T">The type of the array.</typeparam>
    /// <param name="array">The array to split.</param>
    /// <param name="size">The size of the smaller arrays.</param>
    /// <returns>An array containing smaller arrays.</returns>
    public static IEnumerable<IEnumerable<T>> Split<T>(this T[] array, int size)
    {
        for (var i = 0; i < (float)array.Length / size; i++)
        {
            yield return array.Skip(i * size).Take(size);
        }
    }
    

    Moreover, this solution is deferred. Then, simply call split(size) on your array.

    var array = new byte[] {10, 20, 30, 40, 50};
    var splitArray = array.Split(2);
    

    Have fun :)

    0 讨论(0)
  • 2020-12-10 01:21

    I think what you're looking for is the Array class, specifically the Array.Copy static method. You can think of that class as containing the methods that would be instance methods of arrays if C# arrays had methods.

    0 讨论(0)
  • 2020-12-10 01:25

    I had an issue with Linq's Skip() and Take() functions when dealing with arrays with massive amounts of elements (i.e. byte arrays), where element counts are in the millions.

    This approach dramatically reduced split execute times for me.

    public static IEnumerable<IEnumerable<T>> Split<T>(this ICollection<T> self, int chunkSize)
    {
        var splitList = new List<List<T>>();
        var chunkCount = (int)Math.Ceiling((double)self.Count / (double)chunkSize);
    
        for(int c = 0; c < chunkCount; c++)
        {
            var skip = c * chunkSize;
            var take = skip + chunkSize;
            var chunk = new List<T>(chunkSize);
    
            for(int e = skip; e < take && e < self.Count; e++)
            {
                chunk.Add(self.ElementAt(e));
            }
    
            splitList.Add(chunk);
        }
    
        return splitList;
    }
    
    0 讨论(0)
  • 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>(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);
    }
    
    0 讨论(0)
提交回复
热议问题