后端分页

offset 可从0开始

/**
     * 利用subList方法进行分页
     * @param list 分页数据
     * @param limit  页面大小
     * @param offset   当前页面
     */
    public static List<?> pageBySubList(List<?> list, int limit, int offset) throws Exception{
        int totalcount = list.size();
        if(totalcount == 0) {
            return list;
        }else {
            int pagecount = 0;
            List subList;
            int m = totalcount % limit;
            pagecount = totalcount / limit;

            int lastcout = limit+offset;
            if(lastcout<=(totalcount-1)) {
                subList = list.subList(offset,lastcout);
            }else {
                if (m > 0) {
                    pagecount = totalcount / limit;
                    subList = list.subList(pagecount*limit,totalcount);
                } else {
                    pagecount = totalcount / limit;
                    subList = list.subList((pagecount-1)*limit,totalcount);
                }
            }
            return subList;
        }
    }
posted @ 2023-06-26 10:04  学徒陈  阅读(11)  评论(0)    收藏  举报