文章出處

static void Main(string[] args)
        {
            List<String> tarArr = new List<String>();
            tarArr.Add("a");
            tarArr.Add("b");
            tarArr.Add("c");
            tarArr.Add("d");
            tarArr.Add("e");
            tarArr.Add("f");
            //tarArr.Add("g");
            tarArr.Add("h");
            int size = tarArr.Count % 2 == 0 ? tarArr.Count / 2 : tarArr.Count / 2 + 1;
            List<List<String>> result = CreateList(tarArr, size);
        }

public static List<List<T>> CreateList<T>(List<T> targe, int size)
        {
            List<List<T>> listArr = new List<List<T>>();
            //獲取被拆分的數組個數  
            int arrSize = targe.Count() % size == 0 ? targe.Count() / size : targe.Count() / size + 1;
            for (int i = 0; i < arrSize; i++)
            {
                List<T> sub = new List<T>();
                //把指定索引數據放入到list中  
                for (int j = i * size; j <= size * (i + 1) - 1; j++)
                {
                    if (j <= targe.Count() - 1)
                    {
                        sub.Add(targe[j]);
                    }
                }
                listArr.Add(sub);
            }
            return listArr;
        }

  


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()