package com.ruoyi.ibs.list.domain;

import org.apache.poi.ss.formula.functions.T;

import java.util.ArrayList;
import java.util.List;

public class ListUtil {

    public static<T> List<List<T>> splitList(List<T> list, int length){
        List<List<T>> result  = new ArrayList<>();
        if (list==null||list.isEmpty()){
            return result;
        }
        int size = list.size();
        if (size<=length){
            result.add(list);
            return result;
        }
        //分割后的集合个数
        int count  = (size+length-1)/length;
        for (int i = 0; i < count; i++) {
            //计算当前子集合的结束索引
            int toIndex = Math.min((i+1)*length,size);
            List<T> subList = list.subList(i*length,toIndex);
            result.add(subList);
        }
        return result;
    }
}
