C#拆分数组

C#拆分数组,c#,arrays,C#,Arrays,我需要在中点将一个大小不确定的数组拆分为两个单独的数组 数组是使用ToArray()从字符串列表生成的 }为什么不分配两个数组并复制内容 编辑:给你: String[] origin = new String[4]; origin[0] = "zero"; origin[1] = "one"; origin[2] = "two"; origin[3] = "three"; Int32 topSize

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

数组是使用ToArray()从字符串列表生成的


}为什么不分配两个数组并复制内容

编辑:给你:

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

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

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);
}
public void Split(T[]数组,int索引,out T[]第一,out T[]第二){
first=array.Take(index.ToArray();
second=array.Skip(index.ToArray();
}
public void splitmiddpoint(T[]数组,首先输出T[],第二输出T[]{
拆分(数组,数组.长度/2,先出,后出);
}

使用通用拆分方法:

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);
}
publicstaticvoidsplit(T[]source,int index,out T[]first,out T last)
{
int len2=source.Length-索引;
第一个=新的T[指数];
last=新的T[len2];
复制(源,0,第一个,0,索引);
复制(源、索引、最后一个、0、len2);
}

为什么要将
UList
作为ref传递?似乎没有必要这样做

如果我需要这样做,我会使用通用的
Split
方法:

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);
}
public void Split(T[]数组,out T[]左,out T[]右)
{
左=新的T[array.Length/2];
右=新的T[array.Length-left.Length];
数组.Copy(数组,左,左.长度);
复制(数组,左.Length,右,0,右.Length);
}

我想你要找的是
Array
类,特别是
Array.Copy
静态方法。如果C#Array有方法,则可以将该类视为包含数组实例方法的方法。

如果没有Linq,则可以使用数组。复制:

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

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

一个好方法是创建一个泛型/扩展方法来拆分任何数组。这是我的:

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

玩得开心:)

在处理包含大量元素的数组(即字节数组)时,我遇到了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;
}
公共静态IEnumerable拆分(此ICollection self,int chunkSize)
{
var splitList=新列表();
var chunkCount=(int)数学上限((double)self.Count/(double)chunkSize);
for(int c=0;c
如果功能范式是一个问题,这可能有助于:

   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);
     }
  }
公共静态IEnumerable拆分(此IEnumerable seq,Int32 sizeSplits){
Int32 numSplits=(seq.Count()/sizeSplits)+1;
foreach(可枚举范围内的Int32 ns(开始:1,计数:numSplits)){
(Int32开始,Int32结束)=获取索引(ns);

收益率收益率序列,其中((u,i)=>(首先,实际上没有不确定大小的数组。如果是数组,它有一个长度属性。数组大小取决于用户输入的变量数量。我在顶部解释过。可能是重复的谢谢!第二个很完美!这使用Linq序列方法。添加“using System.Linq”名称空间语句。这只是我在玩代码,它是对其中以前的一些内容的记忆。这是拆分单独列表的最佳方法。
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;
}
   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);
     }
  }