# 怎样把list集合划分成指定的几个list集合

0

Anlien-Bei | 初学一级 | 园豆：48

Anlien-Bei 5年前

0

nice 就是这个方法

Anlien-Bei | 园豆：48 (初学一级) | 2018-09-08 15:53

1

List<string> strList = new List<string>() { "1", "2", "3", "4", "5", "6", "7", "8", "9" };
List<List<string>> list = new List<List<string>>();
int i = 0;
int count = 4;
while (i < strList.Count)
{
List<string> item = new List<string>();
if (i + count >= strList.Count)
{
for (int s = 0; s < strList.Count - i; s++)
{
}
break;
}
for (int j = 0; j < count; j++)
{
item.Add(strList[j + i] + ",");
}
i = i + count;
}

``````        foreach (var item in list)
{
for (int p = 0; p < item.Count; p++)
{
Console.Write(item[p]);
}
Console.WriteLine("");
}``````
Anlien-Bei | 园豆：48 (初学一级) | 2018-09-08 15:54
0

``````class Program
{
static void Main(string[] args)
{
var ungroupedList = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
var toalCount = ungroupedList.Count;
int groupCount = 5;
int groupItemCount = (int)Math.Ceiling((double)toalCount / groupCount);
var groupListArray = new List<int>[groupCount];

for (int i = 0; i < groupCount; i++)
{
groupListArray[i] = new List<int>();
var count = Math.Min(toalCount - i * groupItemCount, groupItemCount);
groupListArray[i].AddRange(ungroupedList.GetRange(i * groupItemCount, count));
}

for (var i = 0; i < groupListArray.Length; i++)
{
Console.WriteLine("List" + (i + 1) + ": " + string.Join(",", groupListArray[i]));
}
}
}``````
dudu | 园豆：30925 (高人七级) | 2018-09-08 16:00

string.Join(",", groupListArray[i]) 这个方法好像只适用于字符串吧？

您需要登录以后才能回答，未注册用户请先注册