使用List集合的sublist方法进行分页
//================将数据分页==============
//查询后的数据集合,该对象同样用户截取后的数据集合
List obj = new ArrayList();
//数据总数
int totalCount = 155;
//总的页数
int pageCount = 0;
//每页显示的总数
int endNum = 20;
//当前页码
int startNum = 1;
/*计算出总共能分成多少页*/
if (totalCount % endNum > 0) //数据总数和每页显示的总数不能整除的情况
{
pageCount = totalCount / endNum + 1;
}
else //数据总数和每页显示的总数能整除的情况
{
pageCount = totalCount / endNum;
}
if(totalCount > 0)
{
if(startNum <= pageCount)
{
if(startNum == 1) //当前页数为第一页
{
if(totalCount <= endNum) //数据总数小于每页显示的数据条数
{
//截止到总的数据条数(当前数据不足一页,按一页显示),这样才不会出现数组越界异常
obj = obj.subList(0, totalCount);
}
else
{
obj = obj.subList(0, endNum);
}
}
else
{
//截取起始下标
int fromIndex = (startNum - 1) * endNum;
//截取截止下标
int toIndex = startNum * endNum;
/*计算截取截止下标*/
if ((totalCount - toIndex) % endNum >= 0)
{
toIndex = startNum * endNum;
}
else
{
toIndex = (startNum - 1) * endNum + (totalCount % endNum);
}
if (totalCount >= toIndex)
{
obj = obj.subList(fromIndex, toIndex);
}
}
}
else
{
obj = null;
}
[解决办法]
一般分页都是只查询当前页或者以前内容,不会把所有的内容都查处来
[解决办法]
是的,所有内容都查,当数据量很大的时候……
那就是灾难
[解决办法]
你这是假分页!!~~~~我懂,有时候也是迫不得已~我也有一份
public class ArrayPage {
/**总的结果集*/
private Object[] result = new Object[]{};
/**实际显示的结果集*/
private Object[] displayResult = new Object[]{};
/**起始查询索引*/
private int start;
/**每页显示多少*/
private int pageSize = 10;
/**当前页号*/
private int pageNo;
/**总页数*/
private int pageTotalNo;
/**总条数*/
private int totalCount;
/**是否是第一页*/
private boolean isFirstPage;
/**是否是最后一页*/
private boolean isLastPage;
/**上一页起始索引*/
private int previousPageStart;
/**下一页起始索引*/
private int nextPageStart;
/**最后一页起始索引*/
private int lastPageStart;
public ArrayPage() {
}
public ArrayPage(Object[] result) {
this.result = result;
}
public ArrayPage(Object[] result, int start, int pageSize) {
this(result);
this.start = start;
this.pageSize = pageSize;
}
public Object[] getResult() {
return result;
}
public void setResult(Object[] result) {
this.result = result;
}
/**
* 获取当前起始索引(默认从0开始)
* @return
*/
public int getStart() {
return start;
}
/**
* 设置起始索引值
* @param start
*/
public void setStart(int start) {
this.start = start;
}
/**
* 获取每页显示大小
* @return
*/
public int getPageSize() {
return pageSize;
}
/**
* 设置每页显示条数
* @param pageSize
*/
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
/**
* 获取当前页号
* @return
*/
public int getPageNo() {
return (this.start / this.pageSize) + 1;
}
/**
* 获取总页数
* @return
*/
public int getPageTotalNo() {
return this.getTotalCount() % this.pageSize == 0 ? this.getTotalCount() / this.pageSize : this.getTotalCount() / this.pageSize + 1;
}
/**
* 获取总条数
* @return
*/
public int getTotalCount() {
return this.getResult().length;
}
/**
* 判断是否是最后一页
* @return
*/
public boolean getIsLastPage() {
int expectedSize = this.getPageNo() * this.pageSize;
this.isLastPage = expectedSize >= this.getTotalCount() && expectedSize - this.pageSize <= this.getTotalCount() ;
return this.isLastPage;
}
/**
* 判断是否是第一页
* @return
*/
public boolean getIsFirstPage() {
return this.getPageNo() == 1;
}
/**
* 获取上一页起始索引
* @return
*/
public int getPreviousPageStart() {
return this.start - this.pageSize;
}
/**
* 获取下一页起始索引
*/
public int getNextPageStart() {
return this.start + this.pageSize;
}
/**
* 获取最后一页起始索引
*/
public int getLastPageStart() {
this.lastPageStart = (this.getPageTotalNo() - 1) * this.pageSize;
return this.lastPageStart;
}
/**
* 获取实际需要显示的结果集
* @return
*/
@SuppressWarnings("unchecked")
public Object[] getDisplayResult() {
Object[] t = new Object[10];
if(getIsLastPage()) {
int expectedSize = this.getPageNo() * this.pageSize;
t = new Object[this.getPageSize() - (expectedSize - this.getTotalCount())];
} else {
t = new Object[this.pageSize];
}
System.arraycopy(this.getResult(), this.start, t, 0, t.length);
this.displayResult = t;
return displayResult;
}
public static void main(String[] args) {
Object[] strs = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14"};
ArrayPage page = new ArrayPage(strs, 0, 12);
System.out.println(Arrays.asList(page.getDisplayResult()));
System.out.println("总条数:" + page.getTotalCount());
System.out.println("当前第:" + page.getPageNo() + "页");
System.out.println("总页数:" + page.getPageTotalNo());
System.out.println("是否为最后一页:" + page.getIsLastPage());
}
}