run in this way,   no why,   only for you heart
CSDN博客(点击进入) CSDN
51CTO(点击进入) 51CTO

从list集合中数据以固定数目取出

假设一个接口请求数据每次最多只能10个,现在有105条数据怎么处理。
C#语言:

         List<int> list = new List<int>();
         //简单制造数据
         for (int i = 1; i < 105;i++ ) 
         {
             list.Add(i);            
         }

         int listSize = list.Count();
         //每次最多请求数据个数
         int toIndex = 10;   

         for (int i = 0; i < list.Count(); i += 10)
         {
             if (i + 10 > listSize)
             {        
                 toIndex = listSize - i;
             }
             List<int> newList = new List<int>();
             /**
                 java语言可以用subList()方法,C#中没有看到,就用了下面的for循环方法
                 List<int> newList=list.sublist(i,i+toIndex);
             **/
             for (int j = i; j < i + toIndex;j++ ) 
             {
                 newList.Add(list[j]);
             }
             //这里newList已经拿到了10条数据,可以去做相应的业务
         }
posted @ 2018-09-12 23:30  _小龙人  阅读(2380)  评论(0编辑  收藏  举报