该方法使用的jdk1.6以后自带的分页工具类实现
public static void main(String[] args) {
ArrayList list = new ArrayList<>();
list.add("Luo");
list.add("Tian");
list.add("yan");
list.add("23");
list.add("elegant");
list.add("optimistic");
list.add("mysterious");
System.out.println("分页前数据:"+list);
//分页之后
PageInfo pageInfo = getPageInfo(2, 2, list);
System.out.println("总大小:"+pageInfo.getTotal());
System.out.println("当前页:"+pageInfo.getPageNum());
System.out.println("当前页大小:"+pageInfo.getPageSize());
System.out.println("当前页数据:"+pageInfo.getList());
}
public static <T> PageInfo<T> getPageInfo(int currentPage, int pageSize, List<T> list) {
int total = list.size();
if (total > pageSize) {
int toIndex = pageSize * currentPage;
if (toIndex > total) {
toIndex = total;
}
int totalPage = total % pageSize == 0 ? (total/pageSize) : (total/pageSize)+1;
if (totalPage < currentPage){
list = new ArrayList<>();
}else {
list = list.subList(pageSize * (currentPage - 1), toIndex);
}
}
Page<T> page = new Page<>(currentPage, pageSize);
page.addAll(list);
page.setPages((total + pageSize - 1) / pageSize);
page.setTotal(total);
return new PageInfo<>(page);
}
效果
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/137481.html