/**
* 将一个list均分成n个list,主要通过偏移量来实现的
* @param source
* @return
*/
public static List> averageAssign(List source,int n){
List> result=new ArrayList>();
int remaider=source.size()%n; //(先计算出余数)
int number=source.size()/n; //然后是商
int offset=0;//偏移量
for(int i=0;i
List value=null;
if(remaider>0){
value=source.subList(i*number+offset, (i+1)*number+offset+1);
remaider--;
offset++;
}else{
value=source.subList(i*number+offset, (i+1)*number+offset);
}
result.add(value);
}
return result;
}
@SuppressWarnings("unused")
public static void main(String[] args) {
List integers=new ArrayList<>();
integers.add(1);
integers.add(2);
integers.add(3);
integers.add(4);
integers.add(5);
List> lists=averageAssign(integers, 2);
System.out.println(lists);
}