如何将一个数组列表(size=1000)拆分为多个相同大小(=10)的数组列表?
ArrayList<Integer> results;
如何将一个数组列表(size=1000)拆分为多个相同大小(=10)的数组列表?
ArrayList<Integer> results;
当前回答
这对我很有用
/**
* Returns List of the List argument passed to this function with size = chunkSize
*
* @param largeList input list to be portioned
* @param chunkSize maximum size of each partition
* @param <T> Generic type of the List
* @return A list of Lists which is portioned from the original list
*/
public static <T> List<List<T>> chunkList(List<T> list, int chunkSize) {
if (chunkSize <= 0) {
throw new IllegalArgumentException("Invalid chunk size: " + chunkSize);
}
List<List<T>> chunkList = new ArrayList<>(list.size() / chunkSize);
for (int i = 0; i < list.size(); i += chunkSize) {
chunkList.add(list.subList(i, i + chunkSize >= list.size() ? list.size()-1 : i + chunkSize));
}
return chunkList;
}
例如:
List<Integer> stringList = new ArrayList<>();
stringList.add(0);
stringList.add(1);
stringList.add(2);
stringList.add(3);
stringList.add(4);
stringList.add(5);
stringList.add(6);
stringList.add(7);
stringList.add(8);
stringList.add(9);
List<List<Integer>> chunkList = getChunkList1(stringList, 2);
其他回答
我猜你遇到的问题是命名100个数组列表并填充它们。您可以创建一个数组列表数组,并使用循环填充每个数组列表。
最简单(也是最愚蠢的)的方法是这样的:
ArrayList results = new ArrayList(1000);
// populate results here
for (int i = 0; i < 1000; i++) {
results.add(i);
}
ArrayList[] resultGroups = new ArrayList[100];
// initialize all your small ArrayList groups
for (int i = 0; i < 100; i++) {
resultGroups[i] = new ArrayList();
}
// put your results into those arrays
for (int i = 0; i < 1000; i++) {
resultGroups[i/10].add(results.get(i));
}
创建一个新列表,并使用addAll()方法添加源列表的子列表视图以创建新的子列表
List<T> newList = new ArrayList<T>();
newList.addAll(sourceList.subList(startIndex, endIndex));
这对我很有用
/**
* Returns List of the List argument passed to this function with size = chunkSize
*
* @param largeList input list to be portioned
* @param chunkSize maximum size of each partition
* @param <T> Generic type of the List
* @return A list of Lists which is portioned from the original list
*/
public static <T> List<List<T>> chunkList(List<T> list, int chunkSize) {
if (chunkSize <= 0) {
throw new IllegalArgumentException("Invalid chunk size: " + chunkSize);
}
List<List<T>> chunkList = new ArrayList<>(list.size() / chunkSize);
for (int i = 0; i < list.size(); i += chunkSize) {
chunkList.add(list.subList(i, i + chunkSize >= list.size() ? list.size()-1 : i + chunkSize));
}
return chunkList;
}
例如:
List<Integer> stringList = new ArrayList<>();
stringList.add(0);
stringList.add(1);
stringList.add(2);
stringList.add(3);
stringList.add(4);
stringList.add(5);
stringList.add(6);
stringList.add(7);
stringList.add(8);
stringList.add(9);
List<List<Integer>> chunkList = getChunkList1(stringList, 2);
import org.apache.commons.collections4.ListUtils;
ArrayList<Integer> mainList = .............;
List<List<Integer>> multipleLists = ListUtils.partition(mainList,100);
int i=1;
for (List<Integer> indexedList : multipleLists){
System.out.println("Values in List "+i);
for (Integer value : indexedList)
System.out.println(value);
i++;
}
让我们假设您想要将列表分割为多个块的类作为库类。
所以让我们说这个类被称为“shared”,in应该是final,以确保它不会被扩展。
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public final class Shared {
List<Integer> input;
int portion;
public Shared(int portion, Integer... input) {
this.setPortion(portion);
this.setInput(input);
}
public List<List<Integer>> listToChunks() {
List<List<Integer>> result = new ArrayList<List<Integer>>();
int size = this.size();
int startAt = 0;
int endAt = this.portion;
while (endAt <= size) {
result.add(this.input.subList(startAt, endAt));
startAt = endAt;
endAt = (size - endAt < this.portion && size - endAt > 0) ? (this.size()) : (endAt + this.portion);
}
return result;
}
public int size() {
return this.input.size();
}
public void setInput(Integer... input) {
if (input != null && input.length > 0)
this.input = Arrays.asList(input);
else
System.out.println("Error 001 : please enter a valid array of integers.");
}
public void setPortion(int portion) {
if (portion > 0)
this.portion = portion;
else
System.out.println("Error 002 : please enter a valid positive number.");
}
}
接下来,让我们尝试从另一个持有公共静态void main(String…args)
public class exercise {
public static void main(String[] args) {
Integer[] numbers = {1, 2, 3, 4, 5, 6, 7};
int portion = 2;
Shared share = new Shared(portion, numbers);
System.out.println(share.listToChunks());
}
}
现在,如果输入一个整数数组[1,2,3,4,5,6,7],分区为2。 结果将是[[1,2],[3,4],[5,6],[7]]