Here's a nice extension method for what you want to do, but it's a bit safer because it won't run into out of range issues.

public static IList GroupArray(this T[] array, int groupSize)

{

if (array == null)

throw new ArgumentNullException("array");

if (groupSize <= 0)

throw new ArgumentException("Group size must be greater than 0.", "groupSize");

IList list = new List();

T[] temp = new T[groupSize];

for (int i = 0; i < array.Length; i++)

{

if ((i % groupSize) == 0)

{

temp = new T[groupSize];

list.Add(temp);

}

temp[(i % groupSize)] = array[i];

}

return list;

}

SAMPLE USAGE:

Byte[] myByte = { 1, 2, 3, 4, 4, 2, 3, 4, 2, 5, 3, 4, 4, 2, 6, 3, 4, 5, 3, 3 };

IList myList = myByte.GroupArray(5);

foreach (var item in myList)

{

Console.Write(item + " ");

foreach (var item2 in item)

{

Console.Write(item2);

}

Console.WriteLine();

}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐