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