java list分页_java中利用List的subList方法实现对List分页(简单易学)

以下是介绍利用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 list = new arraylist();

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));

}

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持萬仟网。

希望与广大网友互动??

点此进行留言吧!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值