list.subList进行分页

    public static List<List<Integer>> listPages(List<Integer> list, int pageSize) {
        int totalCount = list.size();
        int pageCount;
        int m = totalCount % pageSize;

        if (m > 0) {
            pageCount = totalCount / pageSize + 1;
        } else {
            pageCount = totalCount / pageSize;
        }

        List<List<Integer>> totalList = new ArrayList<List<Integer>>();
        for (int i = 1; i <= pageCount; i++) {
            if (m == 0) {
                List<Integer> subList = list.subList((i - 1) * pageSize,
                        pageSize * (i));
                totalList.add(subList);
            } else {
                if (i == pageCount) {
                    List<Integer> subList = list.subList((i - 1) * pageSize,
                            totalCount);
                    totalList.add(subList);
                } else {
                    List<Integer> subList = list.subList((i - 1) * pageSize,
                            pageSize * i);
                    totalList.add(subList);
                }
            }
        }

        return totalList;
    }

    public static void main(String[] args) {
        List<Integer> list = new ArrayList<Integer>();
        for (int i = 1; i < 52; i++) {
            list.add(i);
        }
        System.out.println(list);
        List<List<Integer>> totalList = listPages(list, 10);
        int pageNum = 0;// 当前页码,这里是从0开始
        System.out.println(totalList.get(pageNum));// 输出要查看的当前页码的数据
        /*
         * for (int i = 0; i < totalList.size(); i++) {
         * System.out.println(totalList.get(i)); }
         */
    }
参考https://blog.csdn.net/lululove19870526/article/details/46742515

 

posted @ 2018-04-20 11:04  东山磐石西山流云  阅读(320)  评论(0编辑  收藏  举报