一个list分割成若干个list的方法

  1. 按指定大小,分隔集合,将集合按规定个数分为n个部分
public static List> splitList(List list, int len) {  
	if (list == null || list.size() == 0 || len < 1) {  
		return null;  
	}  
  
	List> result = new ArrayList>();  
  
  
	int size = list.size();  
	int count = (size + len - 1) / len;  
  
  
	for (int i = 0; i < count; i++) {  
		List subList = list.subList(i * len, ((i + 1) * len > size ? size : len * (i + 1)));  
		result.add(subList);  
	}  
	return result;  
}  

2.将一个list均分成n个list


	public static  List> averageAssign(List list,int n){
		List> result=new ArrayList>();
		int remaider=list.size()%n;  //(先计算出余数)
		int number=list.size()/n;  //然后是商
		int offset=0;//偏移量
		for(int i=0;i value=null;
			if(remaider>0){
				value=list.subList(i*number+offset, (i+1)*number+offset+1);
				remaider--;
				offset++;
			}else{
				value=list.subList(i*number+offset, (i+1)*number+offset);
			}
			result.add(value);
		}
		return result;
	}

你可能感兴趣的:(java,list,数据结构,leetcode)