Java in the use of the Sublist method of list to achieve the list page (easy to learn) _java

Source: Internet
Author: User

The following is an introduction to using the list of the Sublist method to achieve the list page, nonsense not to say more, directly look at the code to the

/** *//** * List Paging * Implementation: Use list to obtain the sub-list method, implementation of the list page * @author show * @date 2010-1-8 16:27:31 * * * * *  
Va.util.ArrayList;  
 
Import java.util.List; public class Pagemodel {private int page = 1;//current page public int totalpages = 0;/total pages private int Pagere corders;//per page 5 data private int totalrows = 0; Total data private int pagestartrow = 0;//The starting number of each page private int pageendrow = 0; The number of terminations per page to display data private Boolean hasnextpage = false; Whether there is a next page private Boolean haspreviouspage = false;  
 
  Whether there is a previous page private list list;  
 
  Private iterator it;  
   Public Pagemodel (list list, int pagerecorders) {init (list, pagerecorders);//through object set, total number of records is divided}/** *//** * Initialize list and tell the number of records per page of the list * @param list * @param pagerecorders/public void init (list list, int pa  
    gerecorders) {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;  
  }//Determine if you want to page out 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; The public void description () {String Description = "Total number of data:" + this.gettotalrows () + "total Pages:" + this. Gettotalpages () + "Current page is:" + This.getpage () + "whether there is a previous page:" + this.ishaspreviouspage () + "whether there is a next page:" + this.ishasnextpage () + "open  
 
    Number of rows: "+ this.getpagestartrow () +" terminating line number: "+ this.getpageendrow ();  
  System.out.println (description);  
 
    Public List Getnextpage () {page = page + 1;  
 
    Disposepage ();  
    System.out.println ("User's use is the first" + page + "page");  
    This.description ();  
  return getobjects (page);  
    }/** *//** * Processing Paging * * 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);  
    /** *//** * Get the contents of 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) {//To determine if the last page 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;  
  The public void Setpageendrow (int pageendrow) {this.pageendrow = Pageendrow;  
  public int getpagerecorders () {return pagerecorders;  
  The public void setpagerecorders (int pagerecorders) {this.pagerecorders = pagerecorders;  
  public int Getpagestartrow () {return pagestartrow;  
  The public void Setpagestartrow (int pagestartrow) {this.pagestartrow = Pagestartrow; public int gettotalpages () {return TotalpaGes  
  The public void settotalpages (int totalpages) {this.totalpages = TotalPages;  
  public int gettotalrows () {return totalrows;  
  The public void settotalrows (int totalrows) {this.totalrows = totalrows;  
  public Boolean ishaspreviouspage () {return haspreviouspage; }//Test class: Package com.  
 
Gavin.tools.util;  
Import java.util.ArrayList;  
 
Import java.util.List; public class Testpagermodel {public static void main (String args[]) {list<string> List = new ARRAYLIST&L T  
    String> ();  
    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));  }  
 
}

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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: info-contact@alibabacloud.com 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.