42

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

}

3
  • 3
    There's really no such thing as an array of indeterminate size. If it's an array, it has a Length property. Commented Dec 3, 2009 at 17:36
  • 1
    the array size is determined depending on the number of variables input by the user. I did explain at the top. Commented Dec 4, 2009 at 11:51
  • possible duplicate of Split a collection into n parts with LINQ? Commented Feb 18, 2013 at 12:09

10 Answers 10

74

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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! The second is perfect!
This uses Linq sequence methods. Add the "using System.Linq" namespace statement.
13

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

Comments

13

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

Comments

9

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

1 Comment

This is the best way to split separate list.
2

It can be made in a form of a extension with usage of Range and Tuples

public static (T[], T[]) Split<T>(this T[] arr, int index) =>  (arr[..index], arr[index..]);

Example of usage:

var arr = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
var (arr1, arr2) = arr.Split(5);

Comments

1

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

Comments

0

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

1 Comment

That was just me playing with the code, it's a remenant of some previous things that were in there.
0

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.

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.