使用list进行分页

前提入参必须要有全部数据的list,pageNum为当前页数,pageSize为需要分多少页

public static List startPage(List list, Integer pageNum,
                          Integer pageSize) {
        if (list == null) {
            return null;
        }
        if (list.size() == 0) {
            return null;
        }

        Integer count = list.size(); // 记录总数
        Integer pageCount = 1; // 页数
        if (count % pageSize == 0) {
            pageCount = count / pageSize;
        } else {
            pageCount = count / pageSize + 1;
        }

        int fromIndex = 0; // 开始索引
        int toIndex = 0; // 结束索引

        if (pageNum != pageCount) {
            fromIndex = (pageNum - 1) * pageSize;
            if (fromIndex + pageSize > count) {
                toIndex = count;
            } else {
                toIndex = fromIndex + pageSize;
            }
        } else {
            fromIndex = (pageNum - 1) * pageSize;
            toIndex = count;
        }

        List pageList = list.subList(fromIndex, toIndex);

        return pageList;
    }
上一篇:Java-分页工具类


下一篇:c – 如何计算ELF文件中的静态初始化程序?