【问题标题】:How can a large Array be split into smaller arrays?如何将大数组拆分为较小的数组?
【发布时间】:2010-12-09 14:59:02
【问题描述】:

给定一个大数组,如何将其拆分为较小的数组,并将较小数组的大小指定为方法的参数?

例如,给定数字,Split 的实现会是什么?

int[] numbers = new int[7845];

int[][] sectionedNumbers = numbers.Split(1000);

sectionedNumbers.Length; //outputs 8
sectionedNumbers[7].Length; //outputs 845

【问题讨论】:

    标签: c# algorithm


    【解决方案1】:

    您可以使用扩展方法来做到这一点:

    using System;
    
    static class Program
    {
        static T[][] Split<T>(this T[] arrayIn, int length)
        {
            bool even = arrayIn.Length%length == 0;
            int totalLength = arrayIn.Length/length;
            if (!even)
                totalLength++;
    
            T[][] newArray = new T[totalLength][];
            for (int i = 0; i < totalLength;++i )
            {
                int allocLength = length;
                if (!even && i == totalLength - 1)
                    allocLength = arrayIn.Length % length;
    
                newArray[i] = new T[allocLength];
                Array.Copy(arrayIn, i * length, newArray[i], 0, allocLength);
            }
            return newArray;
        }
    
        static void Main(string[] args)
        {
            int[] numbers = new int[8010];
            for (int i = 0; i < numbers.Length; ++i)
                numbers[i] = i;
    
            int[][] sectionedNumbers = numbers.Split(1000);
    
            Console.WriteLine("{0}", sectionedNumbers.Length);
            Console.WriteLine("{0}", sectionedNumbers[7].Length);
            Console.WriteLine("{0}", sectionedNumbers[1][0]);
            Console.WriteLine("{0}", sectionedNumbers[7][298]);
            Console.ReadKey();
        } 
    }
    

    打印出来:

    9
    1000
    1000
    7298
    

    【讨论】:

    • 不要使用'even'变量,只需使用: int totalLength = (arrayIn.Length + length - 1)/length;它稍微短一点,稍微快一点:)
    • @Alex: 会的,只不过以后再用,所以提前计算比较好。
    • 感谢 Reed,这与我想出的方法几乎相同。 Stackoverflow 现在是一个单元测试。
    【解决方案2】:

    这不一定是个好主意,但这里有一个实现,它将这个拆分操作推广到IEnumerable&lt;T&gt;,返回一个IEnumerable&lt;IEnumerable&lt;T&gt;&gt;

    public static IEnumerable<IEnumerable<T>> Split<T>(this IEnumerable<T> input, int size)
    {
        return input.Select((a, i) => new { Item = a, Index = i })
                    .GroupBy( b => (b.Index / size))
                    .Select(c => c.Select(d => d.Item));
    }
    

    【讨论】:

      【解决方案3】:

      Reed 打败了我,但无论如何,这是我的方法:

      public int[][] Split(int[] source, int size)
      {
          int fullArrayCount = source.Length / size;
          int totalArrayCount = fullArrayCount;
          int remainder = source.Length - (fullArrayCount * size);
          if (remainder > 0)
          {
              totalArrayCount++;
          }
          int[][] output = new int[totalArrayCount][];
          for (int i = 0; i < fullArrayCount; i++)
          {
              output[i] = new int[size];
              Array.Copy(source, i * size, output[i], 0, size);
          }
          if (totalArrayCount != fullArrayCount)
          {
              output[fullArrayCount] = new int[remainder];
              Array.Copy(source, fullArrayCount * size,
                  output[fullArrayCount], 0, remainder);
          }
      
          return output;
      }
      

      【讨论】:

        猜你喜欢
        • 2021-03-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-06
        • 1970-01-01
        • 2016-03-12
        • 2013-05-07
        相关资源
        最近更新 更多