android list byte,C# byte[] to List<byte[]>
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 == nu
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();
}
更多推荐
所有评论(0)