Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 每个N元素的新数组_C#_Arrays - Fatal编程技术网

C# 每个N元素的新数组

C# 每个N元素的新数组,c#,arrays,C#,Arrays,我有一个包含200个元素的数组,我正试图将该数组拆分为更小的数组,每N个元素一个数组,这意味着我无法使用.take/.skip命令,我目前尝试了不同的解决方案,如下所示: Parallel.for和Parallel.foreach(如果我能弄明白这一点,那将是最好的) 对于正常的for和foreach循环,但目前仍处于停滞状态,我所能做的就是创建一个新组的静态解决方案,在arrEtikets中为N个元素创建一个新组 string[] arrEtikets = Directory.GetFiles

我有一个包含200个元素的数组,我正试图将该数组拆分为更小的数组,每N个元素一个数组,这意味着我无法使用.take/.skip命令,我目前尝试了不同的解决方案,如下所示:

Parallel.for和Parallel.foreach(如果我能弄明白这一点,那将是最好的)

对于正常的for和foreach循环,但目前仍处于停滞状态,我所能做的就是创建一个新组的静态解决方案,在arrEtikets中为N个元素创建一个新组

string[] arrEtikets = Directory.GetFiles("");

public string[] Group2()
    {
        arrEtikets.Skip(arrEtikets.Length / 10);
        return arrEtikets.Take(arrEtikets.Length / 10).ToArray();
    }

您可以使用Linq,使用
GroupBy
将数组按块大小拆分为数组列表,不使用
Skip
Take

private static List<T[]> SplitToChunks<T>(T[] sequence, int chunkSize)
{
    return sequence.Select((item, index) => new { Index = index, Item = item })
                    .GroupBy(item => item.Index / chunkSize)
                    .Select(itemPerPage => itemPerPage.Select(v => v.Item).ToArray())
                    .ToList();
}

典型的
Skip
+
Take
解决方案可以是这样的:

public static IEnumerable<T[]> SplitArrayWithLinq<T>(T[] source, int size) {
  if (null == source)
    throw new ArgumentNullException("source");
  else if (size <= 0)
    throw new ArgumentOutOfRangeException("size", "size must be positive");

  return Enumerable
    .Range(0, source.Length / size + (source.Length % size > 0 ? 1 : 0))
    .Select(index => source
      .Skip(size * index)
      .Take(size)
      .ToArray());
}
测试(让我们将
[1,2,…8,9]
数组拆分为
4
项块):

var source=Enumerable.Range(1,9).ToArray();
var结果=拆分数组(源,4);
string report=string.Join(Environment.NewLine,
Select(item=>String.Join(“,”,item));
// 1, 2, 3, 4
// 5, 6, 7, 8

//9//你想要一个数组吗?为什么不能使用
Skip
?清楚地说明你的问题。Directory.GetFiles()可能非常慢,这是问题的核心吗?因为每个人都忽略了这一点。您是在数组而不是列表上销售的吗?但是我应该在哪里插入数据数组以供使用,然后是SplitToChunks列表保存了所有数组吗?@Pilsneren
result
保存了所有数组的列表。您可以循环查看
结果
public static IEnumerable<T[]> SplitArrayWithLinq<T>(T[] source, int size) {
  if (null == source)
    throw new ArgumentNullException("source");
  else if (size <= 0)
    throw new ArgumentOutOfRangeException("size", "size must be positive");

  return Enumerable
    .Range(0, source.Length / size + (source.Length % size > 0 ? 1 : 0))
    .Select(index => source
      .Skip(size * index)
      .Take(size)
      .ToArray());
}
public static IEnumerable<T[]> SplitArray<T>(T[] source, int size) {
  if (null == source)
    throw new ArgumentNullException("source");
  else if (size <= 0)
    throw new ArgumentOutOfRangeException("size", "size must be positive");

  int n = source.Length / size + (source.Length % size > 0 ? 1 : 0);

  for (int i = 0; i < n; ++i) {
    T[] item = new T[i == n - 1 ? source.Length - size * i : size];

    Array.Copy(source, i * size, item, 0, item.Length);

    yield return item;
  }
}
var source = Enumerable.Range(1, 9).ToArray();

var result = SplitArray(source, 4);

string report = String.Join(Environment.NewLine, 
  result.Select(item => String.Join(", ", item)));

// 1, 2, 3, 4
// 5, 6, 7, 8
// 9          // <- please, notice that the last chunk has 1 item only
Console.Write(report);