java list<Object>手动分页
枯枫叶 人气:0该方法使用的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); }
效果
加载全部内容