java list分页

package com.css.apps.oa.sw.action;

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

/**
 * Created by pc on 2017/5/27.
 */
public class ListPage {
    public static void main(String[] args) throws Exception {
        List<Object> p = new ArrayList<Object>();
        for(int i = 1; i <= 10; i++){
            p.add(i);
           /* System.out.println(i+ "-" + p.get(i-1));*/
        }
        int totalPageNum = (p.size()  +  9  - 1) / 9;

        for (int j = 1; j <= totalPageNum; j++) {
            System.out.println("---------第"+j+"页------------");
            List<Object> result = page(j,9,p);
            for(int i = 0; i < result.size(); i ++){
                System.out.println(result.get(i));
            }
        }


    }

    /**
     *
     * @param pageNo 当前页码
     * @param pageSize 页数
     * @param list  所有集合
     * @return
     * @throws Exception
     */
    public static List<Object> page(int pageNo, int pageSize, List<Object> list) throws Exception{
        List<Object> result = new ArrayList<Object>();
        if(list != null && list.size() > 0){
            int allCount = list.size();
            int pageCount = (allCount + pageSize-1) / pageSize;
            if(pageNo >= pageCount){
                pageNo = pageCount;
            }
            int start = (pageNo-1) * pageSize;
            int end = pageNo * pageSize;
            if(end >= allCount){
                end = allCount;
            }
            for(int i = start; i < end; i ++){
                result.add(list.get(i));
            }
        }
        return (result != null && result.size() > 0) ? result : null;
    }
}

猜你喜欢

转载自blog.csdn.net/csdn759322423/article/details/72783032