java切分List

public class MyListUtil {

    public static  List> averageAssign(List source, int n) {
        List> result = new ArrayList<>();
        //(先计算出余数)
        int remainder = source.size() % n;
        //然后是商
        int number = source.size() / n;
        //偏移量
        int offset = 0;
        for (int i = 0; i < n; i++) {
            List value;
            if (remainder > 0) {
                value = source.subList(i * number + offset, (i + 1) * number + offset + 1);
                remainder--;
                offset++;
            } else {
                value = source.subList(i * number + offset, (i + 1) * number + offset);
            }
            result.add(value);
        }
        return result;
    }

}
本文由博客一文多发平台 OpenWrite 发布!
个人公众号《骇客与画家》,欢迎关注

你可能感兴趣的:(java)