//方法一:
private static <T> List<List<T>> spliceArrays(List<T> datas, int splitSize) {
if (datas == null || splitSize < 1) {
return null;
}
int totalSize = datas.size();
int count = (totalSize % splitSize == 0) ?
(totalSize / splitSize) : (totalSize/splitSize+1);
List<List<T>> rows = new ArrayList<List<T>>();
for (int i = 0; i < count;i++) {
List<T> cols = datas.subList(i * splitSize,
(i == count - 1) ? totalSize : splitSize * (i + 1));
rows.add(cols);
}
return rows;
}
//方法二:
private static <T> List<List<T>> spliceArrays(List<T> datas, int splitSize) {
if (datas == null || splitSize < 1) {
return null;
}
int totalSize = datas.size();
//获取要拆分子数组个数
int count = (totalSize % splitSize == 0) ?
(totalSize / splitSize) : (totalSize/splitSize+1);
List<List<T>> rows = new ArrayList();
for (int i = 0;i < count;i ++) {
int index = i * splitSize;
List<T> cols = new ArrayList();
int j = 0;
while (j < splitSize && index < totalSize) {
cols.add(datas.get(index++));
j ++;
}
rows.add(cols);
}
return rows;
}
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("1");
list.add("2");
list.add("3");
list.add("4");
List<List<String>> spliceArrays = spliceArrays(list,2);
for (int i = 0; i < spliceArrays.size(); i++) {
System.out.println(spliceArrays.get(i));
}
}