java中利用List的subList方法实现对List分页(简单易学)
作者:ycb1689 时间:2022-06-18 23:33:09
以下是介绍利用List的subList方法实现对List分页,废话不多说了,直接看代码把
/** *//**
* List分页
* 实现:利用List的获取子List方法,实现对List的分页
* @author 显武
* @date 2010-1-8 16:27:31
*
*/
import java.util.ArrayList;
import java.util.List;
public class PageModel {
private int page = 1; // 当前页
public int totalPages = 0; // 总页数
private int pageRecorders;// 每页5条数据
private int totalRows = 0; // 总数据数
private int pageStartRow = 0;// 每页的起始数
private int pageEndRow = 0; // 每页显示数据的终止数
private boolean hasNextPage = false; // 是否有下一页
private boolean hasPreviousPage = false; // 是否有前一页
private List list;
// private Iterator it;
public PageModel(List list, int pageRecorders) {
init(list, pageRecorders);// 通过对象集,记录总数划分
}
/** *//**
* 初始化list,并告之该list每页的记录数
* @param list
* @param pageRecorders
*/
public void init(List list, int pageRecorders) {
this.pageRecorders = pageRecorders;
this.list = list;
totalRows = list.size();
// it = list.iterator();
hasPreviousPage = false;
if ((totalRows % pageRecorders) == 0) {
totalPages = totalRows / pageRecorders;
} else {
totalPages = totalRows / pageRecorders + 1;
}
if (page >= totalPages) {
hasNextPage = false;
} else {
hasNextPage = true;
}
if (totalRows < pageRecorders) {
this.pageStartRow = 0;
this.pageEndRow = totalRows;
} else {
this.pageStartRow = 0;
this.pageEndRow = pageRecorders;
}
}
// 判断要不要分页
public boolean isNext() {
return list.size() > 5;
}
public void setHasPreviousPage(boolean hasPreviousPage) {
this.hasPreviousPage = hasPreviousPage;
}
public String toString(int temp) {
String str = Integer.toString(temp);
return str;
}
public void description() {
String description = "共有数据数:" + this.getTotalRows() +
"共有页数: " + this.getTotalPages() +
"当前页数为:" + this.getPage() +
" 是否有前一页: " + this.isHasPreviousPage() +
" 是否有下一页:" + this.isHasNextPage() +
" 开始行数:" + this.getPageStartRow() +
" 终止行数:" + this.getPageEndRow();
System.out.println(description);
}
public List getNextPage() {
page = page + 1;
disposePage();
System.out.println("用户凋用的是第" + page + "页");
this.description();
return getObjects(page);
}
/** *//**
* 处理分页
*/
private void disposePage() {
if (page == 0) {
page = 1;
}
if ((page - 1) > 0) {
hasPreviousPage = true;
} else {
hasPreviousPage = false;
}
if (page >= totalPages) {
hasNextPage = false;
} else {
hasNextPage = true;
}
}
public List getPreviousPage() {
page = page - 1;
if ((page - 1) > 0) {
hasPreviousPage = true;
} else {
hasPreviousPage = false;
}
if (page >= totalPages) {
hasNextPage = false;
} else {
hasNextPage = true;
}
this.description();
return getObjects(page);
}
/** *//**
* 获取第几页的内容
*
* @param page
* @return
*/
public List getObjects(int page) {
if (page == 0)
this.setPage(1);
else
this.setPage(page);
this.disposePage();
if (page * pageRecorders < totalRows) {// 判断是否为最后一页
pageEndRow = page * pageRecorders;
pageStartRow = pageEndRow - pageRecorders;
} else {
pageEndRow = totalRows;
pageStartRow = pageRecorders * (totalPages - 1);
}
List objects = null;
if (!list.isEmpty()) {
objects = list.subList(pageStartRow, pageEndRow);
}
//this.description();
return objects;
}
public List getFistPage() {
if (this.isNext()) {
return list.subList(0, pageRecorders);
} else {
return list;
}
}
public boolean isHasNextPage() {
return hasNextPage;
}
public void setHasNextPage(boolean hasNextPage) {
this.hasNextPage = hasNextPage;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public int getPageEndRow() {
return pageEndRow;
}
public void setPageEndRow(int pageEndRow) {
this.pageEndRow = pageEndRow;
}
public int getPageRecorders() {
return pageRecorders;
}
public void setPageRecorders(int pageRecorders) {
this.pageRecorders = pageRecorders;
}
public int getPageStartRow() {
return pageStartRow;
}
public void setPageStartRow(int pageStartRow) {
this.pageStartRow = pageStartRow;
}
public int getTotalPages() {
return totalPages;
}
public void setTotalPages(int totalPages) {
this.totalPages = totalPages;
}
public int getTotalRows() {
return totalRows;
}
public void setTotalRows(int totalRows) {
this.totalRows = totalRows;
}
public boolean isHasPreviousPage() {
return hasPreviousPage;
}
}
//测试类:
package com.Gavin.tools.util;
import java.util.ArrayList;
import java.util.List;
public class TestPagerModel {
public static void main(String args[]) {
List<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
list.add("e");
list.add("f");
list.add("g");
list.add("h");
list.add("h");
list.add("i");
list.add("j");
list.add("k");
list.add("l");
list.add("m");
PageModel pm = new PageModel(list, 5);
List sublist = pm.getObjects(3);
for(int i = 0; i < sublist.size(); i++) {
System.out.println(sublist.get(i));
}
System.out.println(sublist.get(0));
}
}
标签:java,list,sublist
0
投稿
猜你喜欢
Java获取文件的路径及常见问题解决方案
2023-04-16 04:24:15
Android使用SharedPreferences存储XML文件的实现方法
2021-07-20 14:24:16
使用logback实现按自己的需求打印日志到自定义的文件里
2022-05-12 16:56:49
SpringBoot集成Shiro进行权限控制和管理的示例
2022-12-31 19:23:21
基于Freemarker和xml实现Java导出word
2022-07-11 23:15:12
基于java的opencv开发过程详解
2022-03-31 20:02:59
C#实现PDF文件添加图片背景
2022-04-03 20:45:35
java 利用反射获取内部类静态成员变量的值操作
2023-03-28 21:30:04
Java实现五子棋AI算法
2022-02-23 05:32:57
Android原生集成RN最新版教程
2021-10-28 15:12:40
SpringBoot的@Value给静态变量注入application.properties属性值
2023-09-20 11:54:07
SpringBoot整合ES解析搜索返回字段问题
2023-01-26 21:39:53
Android ListView 条目多样式展示实例详解
2022-10-22 06:18:41
详解Spring Data JPA动态条件查询的写法
2022-03-17 18:26:06
android APP登陆页面适配的实现
2022-08-27 17:55:57
深入理解Spring Boot的日志管理
2021-11-16 09:58:40
如何从UA分辨出Android设备类型
2023-09-03 00:37:44
C#中动态数组用法实例
2021-11-30 16:42:23
spring boot使用thymeleaf跳转页面实例代码
2021-09-14 09:46:16
Java中常用的设计模式之工厂模式详解
2021-07-04 15:33:37