gpt4 book ai didi

java - 迭代列表时遇到 java.util.ConcurrentModificationException

转载 作者:行者123 更新时间:2023-12-02 17:44:40 25 4
gpt4 key购买 nike

我正在尝试将记录列表拆分为记录子列表。我成功地将列表拆分为子列表,我想查看子列表的内容,但不知何故,我一直遇到此 ConcurrentModificationException。

我的拆分方法:

/**
* @param list - list of results
* @param size - how many sublists
* @return ret - returns a list containing the sublists
* */
public static <T> List<List<T>> split(List<T> list, int size) throws NullPointerException, IllegalArgumentException {
if (list == null) {
throw new NullPointerException("The list parameter is null.");
}
if (size <= 0) {
throw new IllegalArgumentException("The size parameter must be more than 0.");
}

int recordsPerSubList = list.size() / size; // how many records per sublist
List<List<T>> sublists = new ArrayList<List<T>>(size); // init capacity of sublists

// add the records to each sublist
for (int i=0; i<size; i++) {
sublists.add(i, list.subList(i * recordsPerSubList, (i + 1) * recordsPerSubList));
}

// for the remainder records, just add them to the last sublist
int mod = list.size() % recordsPerSubList;
if (mod > 0) {
int remainderIndex = list.size() - mod;
sublists.get(size - 1).addAll(list.subList(remainderIndex, list.size()));
}

return sublists;
}

我在这里称它为:

List<List<QuoteSearchInfo>> ret = Util.split(quoteSearchInfoList, 5);

int fileCounter = 0;
for (List<QuoteSearchInfo> sublist : ret) {
fileCounter++;

String sublistJson = new Gson().toJson(sublist);
filename = JSON_FILE_NAME + fileCounter + JSON_FILE_END;
saveToFile(filename, sublistJson);
AWSManager.getInstance().uploadQuoteSearchJson(filename);
}

^ 在这里,我试图将列表拆分为子列表,以便将它们上传到 S3。

和堆栈跟踪:

java.util.ConcurrentModificationException 
at java.util.SubList.checkForComodification(AbstractList.java:752)
at java.util.SubList.listIterator(AbstractList.java:682)
at java.util.AbstractList.listIterator(AbstractList.java:284)
at java.util.SubList.iterator(AbstractList.java:678)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:95)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:60)
at com.google.gson.Gson.toJson(Gson.java:546)
at com.google.gson.Gson.toJson(Gson.java:525)
at com.google.gson.Gson.toJson(Gson.java:480)
at com.google.gson.Gson.toJson(Gson.java:460)
at com.crover.QuoteSearchRover.execute(QuoteSearchRover.java:41)
at com.crover.CroverMain.execute(CroverMain.java:85)
at com.crover.CroverMain.main(CroverMain.java:35)

最佳答案

sublists.get(size - 1).addAll(list.subList(remainderIndex, list.size()));

sublists.get(size - 1) 是列表子范围的实时 View ,因此当您向其中添加元素时,您也在向原始列表添加元素,位于在您尝试从 list.subList 中取出元素的同时。通常,您不能在迭代列表时修改它。

最简单的解决方案不是将元素添加到 sublists.get(size - 1),而是更新它:

sublists.set(size - 1, 
list.subList(remainderIndex - recordsPerSublist, list.size()));

关于java - 迭代列表时遇到 java.util.ConcurrentModificationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16618188/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com