public static <T> List<List<T>> split(List<T> sourceList, int count) {
List<List<T>> resultList = new ArrayList<>();
int size = sourceList.size();
if (size <= count) {
resultList.add(sourceList);
} else {
int listCount = size / count;
int last = size % count;
for (int i = 0; i < listCount; i++) {
List<T> itemList = new ArrayList<T>();
for (int j = 0; j < count; j++) {
itemList.add(sourceList.get(i * count + j));
}
resultList.add(itemList);
}
if (last > 0) {
List<T> itemList = new ArrayList<T>();
for (int i = 0; i < last; i++) {
itemList.add(sourceList.get(listCount * count + i));
}
resultList.add(itemList);
}
}
return resultList;
}
人生不断地重复着一次一次的问题,把这些问题记录下来,让自己和有需要的人省点时间。