java 假分页

import java.util.ArrayList;
import java.util.List;

import com.alibaba.druid.sql.dialect.mysql.ast.statement.CobarShowStatus;

public class Pager {
	
	/**
	 * 当前页码
	 */
	private int currentPage;
	/**
	 * 总页数
	 */
	private int totalPage;
	/**
	 * 总行数
	 */
	private int totalRows;
	/**
	 * 每页显示条数
	 */
	private int avgRows = 5;
	/**
	 * 原集合
	 */
	private List list;

	public Pager() {
		super();
	}
	
	public Pager(int currentPage, int avgRows, List list) {
		super();
		this.currentPage = currentPage;
		this.avgRows = avgRows;
		this.list = list;
		this.totalRows = list.size();
		this.totalPage = (this.totalRows - 1) / this.avgRows + 1;
	}

	public List getPagerList() {
		List newList = new ArrayList();
		for(int i = (currentPage - 1) * avgRows; i < totalRows && i < currentPage * avgRows; i++) {
			newList.add(list.get(i));
		}
		return newList;
	}
	
	public int getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	public int getTotalPage() {
		return totalPage;
	}
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	public int getAvgRows() {
		return avgRows;
	}
	public void setAvgRows(int avgRows) {
		this.avgRows = avgRows;
	}

	public int getTotalRows() {
		return totalRows;
	}

	public void setTotalRows(int totalRows) {
		this.totalRows = totalRows;
	}

	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}
	
	public static void main(String[] args) {
		List<Integer> integers = new ArrayList<Integer>();
		for(int i=0;i<99;i++){
			integers.add(i);
		}
		
		Pager pager = new Pager(4, 10, integers);
		System.out.println(pager);
	}

	@Override
	public String toString() {
		System.out.println("currentPage="+this.currentPage);
		System.out.println("avgRows="+this.avgRows);
		System.out.println("totalPage="+this.totalPage);
		System.out.println("totalRows="+this.totalRows);
		for(Object i:this.getPagerList()){
			System.out.print(i+",");
		}
		return super.toString();
	}

	
}

 

你可能感兴趣的:(java)