gpt4 book ai didi

java - 子列表抛出的 ConcurrentModificationException

转载 作者:太空狗 更新时间:2023-10-29 22:46:09 24 4
gpt4 key购买 nike

<分区>

我有非常简单的代码:

    List<String> list = new ArrayList<String>();
String a = "a";
String b = "b";
String c = "c";
String d = "d";

list.add(a);
list.add(b);
list.add(c);

List<String> backedList = list.subList(0, 2);
list.add(0, d);
System.out.println("2b: " + backedList);

我通过 list.add(0, d) 得到了 ConcurrentModificationException 异常。所以一般来说,是因为sublist()。我很困惑,因为在 sublist() 的情况下,文档说:

The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa.

你能解释一下问题在哪里吗?

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