java ArrayList.addAll(ArrayList) 有时会抛出 UnsupportedOperationException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7229948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 19:12:45  来源:igfitidea点击:

ArrayList.addAll(ArrayList) throws SOMETIMES UnsupportedOperationException

javalistexceptionarraylist

提问by Rostislav Matl

I have a code that read list from some paged string data. What I do not understand - why the UnsupportedOperationException is thrown on addAll() and why it's kind of random behaviour ?

我有一个从一些分页字符串数据中读取列表的代码。我不明白的是 -为什么在 addAll() 上抛出 UnsupportedOperationException 以及为什么它是一种随机行为?

I know creating target ArrayList and not adding to the returned one solves the issue, I'm looking for better understanding not a fix.

我知道创建目标 ArrayList 而不是添加到返回的列表中可以解决问题,我正在寻找更好的理解而不是修复。

List<Event> eventList = eventTable.getEvents(); // returns ArrayList
while (hasNextPage()) {
  goToNextPage();
  eventList.addAll(eventTable.getEvents());
}

回答by aioobe

List<Event>is not necessarily an ArrayList<Event>. (The opposite is true though.)

List<Event>不一定是ArrayList<Event>. (但事实恰恰相反。)

The reason you get UnsupportedOperationExceptionsometimes, is because eventTable.getEvents()sometimesreturns a list that supports addAlland sometimes it doesn't.

UnsupportedOperationException有时得到的原因是eventTable.getEvents()有时返回支持的列表,addAll有时不返回。

The implementation of getEventscould for instance look like this:

例如,的实现getEvents可能如下所示:

if (noEventsAvailable) {
    return Collections.emptyList();
} else {
    List<Event> toReturn = new ArrayList<Event>();
    // populate list...
    return toReturn;
}

(In your comment you write // returns ArrayList. I don't know where you've got this from, but I know one thing for sure: An ArrayListwill alwayssupport the addAlloperation.)

(在您的评论中,您写道// returns ArrayList。我不知道您从哪里得到这个,但我可以肯定地知道一件事:AnArrayList始终支持该addAll操作。)

The correct way to solve it is, as you mention, to do

正如你所提到的,解决它的正确方法是做

List<Event> eventList = new ArrayList<Event>(eventTable.getEvents());

回答by Suraj Chandran

It depends on the actual implementation of List.

这取决于 的实际实现List

e.g if the underlying list was obtained using Collections.unmodifiableList()then calling addAll()or any other modification method will throw an UnsupportedOperationException.

例如,如果底层列表是使用Collections.unmodifiableList()获得的,则调用addAll()或任何其他修改方法将抛出UnsupportedOperationException.

回答by Jon Skeet

When it throws an exception, it should show you the exact line number and source code file - you should be able to find out exactly whyit's throwing an exception.

当它抛出异常时,它应该向您显示确切的行号和源代码文件 - 您应该能够确切地找出它抛出异常的原因。

My guess is that under certain circumstances, eventTable.getEvents()returns an immutable list, or something like that - but without knowing what eventTableis, it's hard to say for sure. If you can produce a short but complete program which demonstrates the problem, that would make it a lot easier to diagnose.

我的猜测是,在某些情况下,eventTable.getEvents()返回一个不可变的列表,或者类似的东西——但不知道是什么eventTable,很难确定。如果您可以编写一个简短但完整的程序来演示问题,那么诊断就会容易得多。