2014年1月7日星期二

List collection using the sublist method paging

/ / ================ Data paging ==============
/ / data collection query after the object is a collection of the same user data after the interception
List obj = new ArrayList ();
/ / total number of data
int totalCount = 155;
/ / Total number of pages
int pageCount = 0;
/ / display the total number of page
int endNum = 20;
/ / current page
int startNum = 1;
/ * calculate the total number of pages that can be divided into * / case
if (totalCount% endNum> 0) / / Total page displays the total number of data and can not be divisible
{
pageCount = totalCount / endNum + 1;
}
else case / / total number of data can be displayed per page and the total number divisible
{
pageCount = totalCount / endNum;
}
if (totalCount> 0)
{
if (startNum <= pageCount)
{
if (startNum == 1) / / current page is the first page
{ Total
if (totalCount <= endNum) / / data is less than the number of data per page
{
/ / As the number of total data ( currently a lack of data , according to a display ) , so as not to appear abnormal array bounds
obj = obj.subList (0, totalCount);
}
else
{
obj = obj.subList (0, endNum);
}
}
else
{
/ / intercept the starting subscript
int fromIndex = (startNum - 1) * endNum;
/ / intercept deadline subscript
int toIndex = startNum * endNum;
/ * calculate interception deadline subscript * /
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;
}
------ Solution ----------------------------------- ---------
general tab are only query the current page or previous content will not put all the contents are investigated to
------ Solution ------- -------------------------------------
Yes, everything is checked , when the data a large amount of time ......
That is a disaster
------ Solution --------------------------------- -----------
you this is fake page ! ! ~ ~ ~ ~ I know that sometimes is forced ~ I also have a

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

}

------ Solution ------------------------------------- -------
paging is only checked once a page of data , so whether it is displayed in the results , or are good on another query speed .
So the real action lies in each page just check one of the data, rather than check out all the data , then you selectively display to the user.
------ Solution ---------------------------------------- ----
can use SQL paging, paging using JAVA why should it ?
------ Solution ---------------------------------------- ----
upstairs say directly in the database paging, so to receive data directly inside java and then show it wants .

------ For reference only ---------------------------------- -----
engaged ...... now I have so ashamed of his ignorance
database paging, paging framework like hibernate or take to find a page frame

没有评论:

发表评论