java创建list指定长度_java List 按指定长度分割

public static List> splitList(List list, int groupSize){

int length = list.size();

// 计算可以分成多少组

int num = ( length + groupSize - 1 )/groupSize ; // TODO

List> 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 ;

}

标签:分割,java,int,newList,List,length,num,groupSize

public static List> splitList(List list, int groupSize){ int length = list.size(); // 计算可以分成多少组 int num = ( length + groupSize - 1 )/groupSize ; // TODO List> 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 ; } 标签:分割,java,int,newList,List,length,num,groupSize
经验分享 程序员 微信小程序 职场和发展