假設一個接口請求數據每次最多只能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條數據,可以去做相應的業務
}
