Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
迭代列表时运行到java.util.ConcurrentModificationException_Java_List_Concurrentmodification - Fatal编程技术网

迭代列表时运行到java.util.ConcurrentModificationException

迭代列表时运行到java.util.ConcurrentModificationException,java,list,concurrentmodification,Java,List,Concurrentmodification,我正在尝试将记录列表拆分为记录子列表。我成功地将列表拆分为子列表,我想查看子列表的内容,但不知何故,我一直遇到ConcurrentModificationException 我的拆分方法: /** * @param list - list of results * @param size - how many sublists * @return ret - returns a list containing the sublists * */ p

我正在尝试将记录列表拆分为记录子列表。我成功地将列表拆分为子列表,我想查看子列表的内容,但不知何故,我一直遇到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;
    }
subList.get(size-1)
是列表子范围的实时视图,因此当您向其中添加元素时,您也将元素添加到原始列表中,同时尝试从
list.subList
中取出元素。通常,在对列表进行迭代时,不能对其进行修改

最简单的解决方案不是将元素添加到
子列表中。get(size-1)
,而只是更新它:

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

发布异常堆栈跟踪。@Pangea补充道,当我试图对列表进行gson时,似乎出现了问题。此异常通常意味着列表在迭代时被修改。GSON在引擎盖下做什么?您可能还想将公司名称更改为其他名称。@ZiyaoWei公司名称?@ZiyaoWei哈哈,谢谢!目前在我的代码中,我有5个列表,其中前4个列表的长度相等。最后一个(第5个)列表将包含其他列表的长度加上任何剩余记录。该代码不会取代第五个列表吗?这看起来是答案,但是stacktrace说异常不会发生在
split
方法中;有什么评论吗?
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.set(size - 1, 
   list.subList(remainderIndex - recordsPerSublist, list.size()));