zoukankan      html  css  js  c++  java
  • java list 切分

    package com.founder.tongyin.util;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * TODO
     *
     * @ClassName: ListUtil
     * @author: dh
     * @since: 2020/8/3 15:44
     */
    public class ListUtil {
    
        public static <T> List<List<T>> splitList(List<T> list, int groupSize){
            int length = list.size();
            int num = ( length + groupSize - 1 )/groupSize ;
            List<List<T>> newList = new ArrayList<>(num);
            for (int i = 0; i < num; i++) {
                int fromIndex = i * groupSize;
                int toIndex = (i+1) * groupSize < length ? ( i+1 ) * groupSize : length ;
                newList.add(list.subList(fromIndex,toIndex)) ;
            }
            return  newList ;
        }
    
    
        public static <T> List<List<T>> splitListToNum(List<T> list, int num){
            int length = list.size();
            int groupSize = (int)Math.ceil(length*1.0 / num);
            List<List<T>> newList = new ArrayList<>(num);
            for (int i = 0; i < num; i++) {
                int fromIndex = i * groupSize;
                int toIndex = (i+1) * groupSize < length ? ( i+1 ) * groupSize : length ;
                newList.add(list.subList(fromIndex,toIndex)) ;
            }
            return  newList ;
        }
    
    }
  • 相关阅读:
    C# MATLAB混编(二)
    C# MATLAB混编(一)
    C#编程.循环的中断
    WPS 常用操作
    PowerDesigner 操作手册
    软件开发-零散问题
    代码优化
    layui 前端UI框架
    css 样式渲染
    linux 操作系统
  • 原文地址:https://www.cnblogs.com/dhName/p/14560517.html
Copyright © 2011-2022 走看看