2

我正在尝试将记录列表拆分为记录子列表。我成功地将列表拆分为子列表,我想查看子列表的内容,但不知何故我一直遇到这个 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)
4

1 回答 1

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

sublists.get(size - 1)是列表子范围的实时视图,因此当您向其中添加元素时,您也在将元素添加到原始列表中,同时您试图从list.subList. 通常,您无法在迭代列表时对其进行修改。

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

sublists.set(size - 1, 
   list.subList(remainderIndex - recordsPerSublist, list.size()));
于 2013-05-17T21:36:26.137 回答