ArrayList 分隔List集合,按指定大小,将集合分成多个

分页的原理

 //初始化一个目标list 
List arrayList = new ArrayList<>();
        for (int i = 0; i <= 231; i++) {
            arrayList.add(i + "");
        }

   //分割多少,计算一共会有多少页
        int pageSize = 10;
        int total = (arrayList.size() + pageSize - 1) / pageSize;
        System.out.println(total);
   //分割目标list
        for (int i = 0; i < total; i++) {
            List temp = new ArrayList<>();
            for (int j = 0; j < pageSize; j++) {
                if (arrayList.size() < pageSize && arrayList.size() > 0) {
                    temp.addAll(arrayList);
                    break;
                } else if (arrayList.size() > pageSize) {
                    temp.add(arrayList.get(j));
                }
            }
           //在目标集合里移除掉已经分割出来的list
            arrayList.removeAll(temp);
            //temp就是分割出来的list todosomething?
            System.out.println(temp.toString());//每次分割出来的list temp 
        }

 

你可能感兴趣的:(java)