package cn.com.ty.lift.ud.utils;

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

/**
 * 分页
 */
public class PageUtil {
    /**
     * List集合分页
     * @param list
     * @param pageNum 页码
     * @param pageSize 每页多少条数据
     * @return
     */
    public static List startPage(List list, Integer pageNum,
                                 Integer pageSize) {
        if (list == null) {
            return null;
        }
        if (list.size() == 0) {
            return null;
        }

        // 记录总数
        int count = list.size();
        // 页数
        int pageCount = 0;
        if (count % pageSize == 0) {
            pageCount = count / pageSize;
        } else {
            pageCount = count / pageSize + 1;
        }
        // 开始索引
        int fromIndex = 0;
        // 结束索引
        int toIndex = 0;

        if (pageNum != pageCount) {
            fromIndex = (pageNum - 1) * pageSize;
            toIndex = fromIndex + pageSize;
        } else {
            fromIndex = (pageNum - 1) * pageSize;
            toIndex = count;
        }

        List pageList = list.subList(fromIndex, toIndex);

        return pageList;
    }

    public  static void main(String[] args){
        List list = new ArrayList();
        for(int a = 23;a>0;a--){
            list.add(a);
        }
        System.out.println(list.toString());
        System.out.println("--------间隔---------");
        List list1 = PageUtil.startPage(list, 2, 5);
        System.out.println(list1.toString());

    }
}
