C#拆分数组

The*_*God 22 c# arrays

我需要在中点将一个不确定大小的数组拆分成两个独立的数组.

使用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);
    }
}
Run Code Online (Sandbox Code Playgroud)

}

Jar*_*Par 52

您可以使用以下方法将数组拆分为2个单独的数组

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);
}
Run Code Online (Sandbox Code Playgroud)

  • 这使用 Linq 序列方法。添加“使用 System.Linq”命名空间语句。 (2认同)

Cec*_*ame 11

使用通用拆分方法:

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);
}
Run Code Online (Sandbox Code Playgroud)


Zen*_*ulz 7

我还想添加一个解决方案,将一个数组拆分成几个包含确定数量单元格的较小数组.

一种不错的方法是创建一个通用/扩展方法来拆分任何数组.这是我的:

/// <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);
    }
}
Run Code Online (Sandbox Code Playgroud)

而且,该解决方案被推迟.然后,只需调用split(size)您的阵列.

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

玩得开心 :)


小智 6

在处理具有大量元素(即字节数组)的数组时,我遇到了Linq的Skip()和Take()函数的问题,其中元素数量是数百万.

这种方法大大减少了我的分割执行时间.

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;
}
Run Code Online (Sandbox Code Playgroud)