数组按照一定数量分组的方法,
/**
* 数组按照一定数量分组
*
* @param array
* @return
*/
public List<List<String>> spiltarray(String[] array) {
int num = 6;
int count = array.length % num == 0 ? array.length / num : array.length / num + 1;
List<List<String>> arrayList = new ArrayList<>();
for (int i = 0; i < count; i++) {
int index = i * num;
List<String> list = new ArrayList<>();
int j = 0;
while (j < num && index < array.length) {
list.add(array[index++]);
j++;
}
arrayList.add(list);
}
// for (List<String> list : arrayList) {
// //System.out.println(Arrays.toString(list.toArray()));
// }
return arrayList;
}
本文介绍如何使用Java将数组有效地分成多个子数组,每个子数组包含指定数量的元素,适用于后端开发中的数据处理场景。

1万+

被折叠的 条评论
为什么被折叠?



