zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

直接在代码里面对list集合进行分页

2023-03-15 21:57:22 时间
 public List pageList(List resList, int page, int limit) {
        List resultList = null;
        if (resList != null && resList.size() != 0) {
            if (page != 0 && limit != 0) {
                Integer count = resList.size(); // 记录总数
                Integer pageCount = 0; // 页数
                if (count % limit == 0) {
                    pageCount = count / limit;
                } else {
                    pageCount = count / limit + 1;
                }

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

                if (page != pageCount) {
                    fromIndex = (page - 1) * limit;
                    toIndex = fromIndex + limit;
                } else {
                    fromIndex = (page - 1) * limit;
                    toIndex = count;
                }
                resultList = resList.subList(fromIndex, toIndex);
            } else {
                resultList = resList;
            }
        }
        return resultList;
    }