C# 对一维数组进行分组,每组固定数量
效果:
[1,2,3,4,5,6]
拆分为每个数组内2个数字
[[1,2],[3,4],[5,6]]
/// <summary>
/// 根据数量对数组进行分组
/// </summary>
/// <param name="list"></param>
/// <param name="size">数量</param>
private static List<List<T>> GroupListBySize<T>(List<T> list, int size)
{
var listArr = new List<List<T>>();
int arrSize = list.Count() % size == 0 ? list.Count() / size : list.Count() / size + 1;
for (int i = 0; i < arrSize; i++)
{
var sub = new List<T>();
for (int j = i * size; j <= size * (i + 1) - 1; j++)
{
if (j <= list.Count() - 1)
{
sub.Add(list[j]);
}
}
listArr.Add(sub);
}
return listArr;
}