Simple paging algorithm

Source: Internet
Author: User

In most programs, the paging algorithm is required. Obviously, the paging algorithm must be independent. If the business logic is also mixed together, it is a pain to modify it. In fact, the design of the paging algorithm is like solving the equation. Input several variables to find several other variables. The following is a simple design: the total number of input records, the current page, the number of redirect pages, and the index of the output request. Of course, you can also modify the algorithm to output other information.

Package paging; public class page {/*** the number of records displayed on each page, that is, the number of your requests */public static final int pagesize = 20; /*** total number of records */private int records;/*** count the current page from 1 */private int now_page; /*** total page number */Public int pages;/*** request location, which indicates the number of records you have requested */Public int request_index; /*** whether the Page Object is successfully constructed */Public Boolean OK = true; Public page () {}/ *** is generally used in the first request, because you do not know the total number of records */Public page (INT now_page) {This (pagesize, now_page, 0 );} Public page (INT now_page, int go_count) {This (pagesize, now_page, go_count );} /***** @ Param records * Total Record Book * @ Param now_page * index of the current page * @ Param go_count * jumps to several pages (negative numbers are supported) */Public page (INT records, int now_page, int go_count) {If (now_page-1) * pagesize> = records | now_page <= 0) {system. out. println ("failed to construct page-1! "); OK = false;} If (now_page + go_count <= 0 | (now_page + go_count-1) * pagesize> = records) {system. out. println ("failed to construct page-2! "); OK = false;} This. now_page = now_page; this. records = records; this. request_index = (this. now_page + go_count-1) * pagesize; If (this. records % pagesize = 0) {This. pages = This. records/pagesize;} else {This. pages = This. records/pagesize + 1 ;}@ overridepublic string tostring () {return "pages:" + pages + "" + "request_index:" + request_index ;}}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.