List数组指定切割

废话不多说直接上代码!

public static void main(String[] args) {
        List<Integer> integers = Arrays.asList(1,2,3,4,5,6,7,8,9);
 
        List<List<Integer>> chopped = chopped(integers, 5);
 
        for (List<Integer> integerList : chopped) {
            System.out.println(integerList);
        }
 
    }
 
    // chops a list into non-view sublists of length L
    static <T> List<List<T>> chopped(List<T> list, final int L) {
        List<List<T>> parts = new ArrayList<>();
        final int N = list.size();
        for (int i = 0; i < N; i += L) {
            parts.add(new ArrayList<>(
                    list.subList(i, Math.min(N, i + L)))
            );
        }
        return parts;
    }

posted @ 2020-10-07 10:04  张什么锋  阅读(952)  评论(0编辑  收藏  举报