如题:有这样的一组数据 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16
想归类成如果下格式
{1,5,9,13}
{2,6,10,14}
{3,7,11,15}
{4,8,12,16}
//数据源 var array = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; //定义一个集合 var arrayList = new List<List<int>>(); //每隔几个数字归一类 这里是每隔4个数字归一类 for (int i = 0; i < 4; i++) { //生成4个对象集合 arrayList.Add(new List<int>()); } int n = 0; while (n < array.Length) { /* * 有点难理解的地方 * 循环arrayList 这个集合,刚刚在上面定义,该集合中有 4个 List<int> 对象 * 也就是 当跳出循环是当前 n 是4 * 然后因为循环的 arrayList ,item.add 当前的item是arrayList中从0开始....3 * 有因为item.add(array[n]) 这里的array[0]取的数据源中的数据0....3 然后又和arrayList 对应上了 * * 最终结果是arrayList[0]=array[0] * arrayList[1]=array[1] * arrayList[2]=array[2] * arrayList[3]=array[3] */ foreach (List<int> item in arrayList) { //防止溢出,也许数据源总数整除不了4 if (n < array.Length) item.Add(array[n]); n++; } }