我試圖將記錄列表拆分成記錄的子列表。我成功地將列表拆分爲子列表,我想查看子列表的內容,但不知何故我仍然遇到此ConcurrentModificationException。迭代列表時碰到java.util.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)
後的異常堆棧跟蹤。 –
@Pangea補充說,當我嘗試列表列表時,看起來有些問題。 – iCodeLikeImDrunk
這個異常通常意味着該列表在被迭代時被修改。 GSON在做什麼?你也可能想把公司名稱改成不同的東西。 –