java List 按指定长度分割


    public static <T> List<List<T>> splitList(List<T> list, int groupSize){ int length = list.size(); // 计算可以分成多少组
        int num = ( length + groupSize - 1 )/groupSize ; // TODO 
        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 ; }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM