package com.focusorder.mirror.utils;
import java.util.ArrayList;
import java.util.List;
public class SplitArray {
public static void main(String[] args) throws InterruptedException {
List<String> tarArr = new ArrayList<String>();
for (int i = 0; i < 752; i++) {
tarArr.add("a" + i);
}
List<List<String>> result = createListByNumber(tarArr, 3);
for (List<String> subArr : result) {
System.err.println(subArr.size());
}
}
/**
* 分解数组:按每个子数组的大小,List=100,指定子数组大小=50,截取成共2个数组
* @param targe
* @param size
* @return
*/
public static <T> List<List<T>> createListBySize(List<T> targe, int size) {
List<List<T>> listArr = new ArrayList<List<T>>();
// 获取被拆分的数组个数
int arrSize = targe.size() % size == 0 ? targe.size() / size : targe.size() / size + 1;
for (int i = 0; i < arrSize; i++) {
List<T> sub = new ArrayList<T>();
// 把指定索引数据放入到list中
for (int j = i * size; j <= size * (i + 1) - 1; j++) {
if (j <= targe.size() - 1) {
sub.add(targe.get(j));
}
}
listArr.add(sub);
}
return listArr;
}
/**
* 分解数组:按每个子数组的个数,List=100,指定子数组个数=5,截取成每个数组大小=20
* @param targe
* @param number
* @return
*/
public static <T> List<List<T>> createListByNumber(List<T> targe, int number) {
if (targe.size() < number) return null;
List<List<T>> listArr = new ArrayList<List<T>>();
// 获取被拆分的数组个数
for (int i = 0; i < number; i++) {
List<T> sub = targe.subList(i * (targe.size() / number), i == number - 1 ? (targe.size()) : i * (targe.size() / number) + targe.size() / number);
listArr.add(sub);
}
return listArr;
}
}
Comments | NOTHING