• list 集合 分页


    public class ListUtil {
        // 方式一 ,根据分页条数,返回 一个 新的 list 集合
        public static  <T> List<List<T>> splitList(List<T> list, int pageSize)
        {
    
            int listSize = list.size();
            int page = (listSize + (pageSize - 1)) / pageSize;
    
            List<List<T>> listArray = new ArrayList<List<T>>();
            for (int i = 0; i < page; i++)
            {
                List<T> subList = new ArrayList<T>();
                for (int j = 0; j < listSize; j++)
                {
                    int pageIndex = ((j + 1) + (pageSize - 1)) / pageSize;
                    if (pageIndex == (i + 1))
                    {
                        subList.add(list.get(j));
                    }
                    if ((j + 1) == ((j + 1) * pageSize))
                    {
                        break;
                    }
                }
                listArray.add(subList);
            }
            return listArray;
        }
    }

    方式一:
    public static  void fenye(List list,int pagesize){
        int totalcount=list.size();
        int pagecount=0;
        int m=totalcount%pagesize;
        if  (m>0){
            pagecount=totalcount/pagesize+1;
        }else{
            pagecount=totalcount/pagesize;
        }
        for(int i=1;i<=pagecount;i++){
            if (m==0){
                List<Integer> subList= list.subList((i-1)*pagesize,pagesize*(i));
                System.out.println(subList);
            }else{
                if (i==pagecount){
                    List<Integer> subList= list.subList((i-1)*pagesize,totalcount);
                    System.out.println(subList);
                }else{
                    List<Integer> subList= list.subList((i-1)*pagesize,pagesize*(i));
                    System.out.println(subList);
                }
            }
        }
    }

    List<Integer> list=new ArrayList<Integer>();
    for(int i=1;i<52;i++){
        list.add(i);
    }
    fenye(list,10);

    方式二:
     public <T> List<List<T>> splitList(List<T> list, int pageSize)
        {

            int listSize = list.size();
            int page = (listSize + (pageSize - 1)) / pageSize;

            List<List<T>> listArray = new ArrayList<List<T>>();
            for (int i = 0; i < page; i++)
            {
                List<T> subList = new ArrayList<T>();
                for (int j = 0; j < listSize; j++)
                {
                    int pageIndex = ((j + 1) + (pageSize - 1)) / pageSize;
                    if (pageIndex == (i + 1))
                    {
                        subList.add(list.get(j));
                    }
                    if ((j + 1) == ((j + 1) * pageSize))
                    {
                        break;
                    }
                }
                listArray.add(subList);
            }
            return listArray;
        }
    方式三:
     public SearchResult paging(List<T> list, int pageNumber, int pageSize) throws Exception{
            List<T> pageList = new ArrayList<T>();
            int currIdx = (pageNumber > 1 ? (pageNumber -1) * pageSize : 0);
            for (int i = 0; i < pageSize && i < list.size() - currIdx; i++){
                T listNew = list.get(currIdx + i);
                pageList.add(listNew);
            }
            SearchResult<T> searchResult = new SearchResult<T>();
            searchResult.setList(pageList);
            searchResult.setTotal(list.size());
            if (searchResult.getTotal()%pageSize == 0){
                searchResult.setPages((int)searchResult.getTotal()/pageSize);
            }else {
                searchResult.setPages((int)searchResult.getTotal()/pageSize + 1);
            }
            searchResult.setPageNum(pageNumber);
            searchResult.setPageSize(pageSize);
            searchResult.setSize(pageList.size());
            //是否是第一页
            if (pageNumber == 1){
                searchResult.setIsFirstPage(true);
            }else {
                searchResult.setIsFirstPage(false);
            }
            //是否是最后一页
            if ((searchResult.getTotal() - pageNumber*pageSize) > 0){
                searchResult.setIsLastPage(false);
            }else {
                searchResult.setIsLastPage(true);
            }
            return searchResult;
        }
    方式四:
     List<Integer> list = new ArrayList<>();
            list.add(1);
            list.add(2);
            list.add(3);
            list.add(4);
            list.add(5);
            
            int page = 2;//相当于pageNo
            int count = 20;//相当于pageSize
            int size = list.size();
            int pageCount=size/count;
            int fromIndex = count * (page - 1);
            int toIndex = fromIndex + count;
            if (toIndex >= size) {
                toIndex = size;
            }
            if(page>pageCount+1){
                fromIndex=0;
                toIndex=0;
            }
            
            System.out.println(list.subList(fromIndex, toIndex));

  • 相关阅读:
    react 实现路由按需加载
    vue-router 与 react-router 设计理念上的区别
    create-react-app 知识点
    ElementUI(vue UI库)、iView(vue UI库)、ant design(react UI库)中组件的区别
    create-react-app 搭建的项目中,让 antd 通过侧边栏导航 Menu 的 Menu.Item 控制 Content 部分的变化
    react-router v4.0 知识点
    prop-types:该第三方库对组件的props中的变量进行类型检测
    002_mtr_a network diagnostic tool
    006_netstat中state详解
    003_监测域名证书过期时间
  • 原文地址:https://www.cnblogs.com/whm-blog/p/11199954.html
Copyright © 2020-2023  润新知