将集合的数据分页显示>>>一个不错的分页类!还有缺陷希望大家帮我改改..

package com.yida.common;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * 将集合中的数据分页显示
 *
 * @author Administrator
 *
 */
@SuppressWarnings("unchecked")
public class CollectionToPatination<T>
{

 private Integer pageCount;

 private Integer currentPage;

 private Integer size;

 protected Integer getSize(Integer size)
 {
  if (null==size || size.intValue() < 1)
  {
   this.size = 1;
  }
  else
  {
   this.size = size;
  }
  return this.size;
 }

 public Integer getCurrentPage()
 {
  return currentPage;
 }

 private void setCurrentPage(Integer page)
 {
  // 判断是否在合法范围内,如果不在自动指定
  if (page.intValue() > pageCount.intValue())
  {
   this.currentPage = pageCount;
  }
  else if (page.intValue() < 1)
  {
   this.currentPage = 1;
  }
  else
  {
   this.currentPage = page;
  }
 }

 public Integer getPageCount()
 {
  return pageCount;
 }

 private void setPageCount(Collection coll)
 {
  this.pageCount = coll.size() % size.intValue() == 0 ? coll.size()
    / size : coll.size() / size + 1;
 }

 /**
  * 根据传来的数据来分页显示数量,使用截取
  *
  * @param lst
  * @param size
  * @param page
  *            这个必须为正整数
  * @return
  */
 @SuppressWarnings("unused")
 public List<T> parseCollectionToPatination(Collection coll, Integer size,
   Integer page)
 {
  // 不能放乱
  this.getSize(size);
  this.setPageCount(coll);
  this.setCurrentPage(page);
  System.out.println("PD"+(page.intValue()>currentPage.intValue())+"   cur:"+this.getCurrentPage());
  List l = new ArrayList();

  // 取得要得到的开始与结束下标
  int start = (this.getCurrentPage().intValue() - 1) * size.intValue()
    + 1;
  int end = start + size - 1;

  int s = coll.size();

  // 遍历集合,查出要取得的数量
  for (int h = start; h <= end; h++)
  {
   int idex = h - 1;
   try
   {
    l.add(coll.toArray()[idex]);
   }
   catch (IndexOutOfBoundsException ie)
   {
   }
  }
  return ((List<T>) l);
 }

 public static void main(String[] args)
 {
  List l = new ArrayList();
  // Set l=new TreeSet();
  l.add("唐亮1");
  l.add("唐亮4");
  l.add("唐亮5");
  l.add("唐亮2");
  l.add("唐亮6");
  l.add("唐亮3");
  Collections.sort(l);
  System.out.println(l.size());
  for (Object o : new CollectionToPatination<Object>()
    .parseCollectionToPatination(l, 2, 3))
  {
   System.out.println(o);
  }
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值