简体   繁体   中英

C# Splitting An Array

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().

        public void AddToList ()
        {
            bool loop = true;
            string a = "";

            Console.WriteLine("Enter a string value and press enter to add it to the list");
            while (loop == true)
            {
                a = Console.ReadLine();

                if (a != "")
                {
                    mylist.Add(a);
                }
                else
                {
                    loop = false;
                }
            }

        }

        public void ReturnList()
        {
            string x = "";
            foreach (string number in mylist)
            {
                x = x + number + " ";
            }
            Console.WriteLine(x);
            Console.ReadLine();
        }

    }

    class SplitList
    {
        public string[] sTop;
        public string[] sBottom;

        public void Split(ref UList list)  
        {
            string[] s = list.mylist.ToArray();

            //split the array into top and bottom halfs

        }
    }

    static void Main(string[] args)
    {
        UList list = new UList();
        SplitList split = new SplitList();

        list.AddToList();
        list.ReturnList();

        split.Split(ref list);
    }
}

}

You could use the following method to split an array into 2 separate arrays

public void Split<T>(T[] array, int index, out T[] first, out T[] second) {
  first = array.Take(index).ToArray();
  second = array.Skip(index).ToArray();
}

public void SplitMidPoint<T>(T[] array, out T[] first, out T[] second) {
  Split(array, array.Length / 2, out first, out second);
}

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);
}

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 :)

I had an issue with Linq's Skip() and Take() functions when dealing with arrays with massive amounts of elements (ie 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;
}

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);
}

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);

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);
}

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.

If functional paradigm is a concern, this might help:

   public static IEnumerable<IEnumerable<T>> Split<T>(this IEnumerable<T> seq, Int32 sizeSplits) {
     Int32 numSplits = (seq.Count() / sizeSplits) + 1;
     foreach ( Int32 ns in Enumerable.Range(start: 1, count: numSplits) ) {
        (Int32 start, Int32 end) = GetIndexes(ns);
        yield return seq.Where((_, i) => (start <= i && i <= end));
     }

     (Int32 start, Int32 end) GetIndexes(Int32 numSplit) {
        Int32 indBase1 = numSplit * sizeSplits;
        Int32 start = indBase1 - sizeSplits;
        Int32 end = indBase1 - 1;
        return (start, end);
     }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM