zoukankan      html  css  js  c++  java
  • 将list按指定大小分为n组

    public static <T> List<List<T>> splitList(List<T> list,int groupSize){
    List<List<T>> result = Lists.newArrayList();
    int length = list.size();
    //每组大小
    StringBuilder stringBuilder = null;
    if (length > groupSize){


    int lastIndex = 0;
    int fromIndex = 0;
    int endIndex = 0;
    //计算分多少组
    int num = (length + groupSize -1 ) / groupSize;
    for(int i = 0;i<num;i++){
    //第i组的起始index
    fromIndex = i *groupSize;
    //第i组的结束index
    endIndex = (i+1) * groupSize < length ? (i+1) * groupSize : length;

    List<T> tempList = Lists.newArrayList();
    for (int j = fromIndex;j<endIndex;j++){
    tempList.add(list.get(j));
    }


    result.add(tempList);
    }
    }else{
    result.add(list);
    }
    return result;
    }
  • 相关阅读:
    Linux进程间通信(IPC)
    mq_setattr
    mq_getattr
    mq_unlink
    mq_receive
    mq_send
    mq_close
    POSIX消息队列
    mq_open
    C语言关键字
  • 原文地址:https://www.cnblogs.com/heamin/p/14296432.html
Copyright © 2011-2022 走看看