SplitList-分割数组


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

/**
 * Created on 2022/11/17.
 *
 * @author lichuanming
 */
public class SplitList {
    public static <T> List<List<T>> splitListByNum(List<T> list, int subNum) {
        List<List<T>> tNewList = new ArrayList<List<T>>();
        int priIndex = 0;
        int lastPriIndex = 0;
        int insertTimes = list.size()/subNum;
        List<T> subList = new ArrayList<>();
        for (int i = 0;i <= insertTimes;i++) {
            priIndex = subNum*i;
            lastPriIndex = priIndex + subNum;
            if (i == insertTimes) {
                subList = list.subList(priIndex,list.size());
            } else {
                subList = list.subList(priIndex,lastPriIndex);
            }
            if (subList.size() > 0) {
                tNewList.add(subList);
            }
        }
        return tNewList;
    }
}

posted @ 2022-11-17 15:27  小傻孩丶儿  阅读(179)  评论(0编辑  收藏  举报