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 ?
I know creating target ArrayList and not adding to the returned one solves the issue, I’m looking for better understanding not a fix.
List<Event> eventList = eventTable.getEvents(); // returns ArrayList
while (hasNextPage()) {
goToNextPage();
eventList.addAll(eventTable.getEvents());
}
List<Event>is not necessarily anArrayList<Event>. (The opposite is true though.)The reason you get
UnsupportedOperationExceptionsometimes, is becauseeventTable.getEvents()sometimes returns a list that supportsaddAlland sometimes it doesn’t.The implementation of
getEventscould for instance look like this:(In your comment you write
// returns ArrayList. I don’t know where you’ve got this from, but I know one thing for sure: AnArrayListwill always support theaddAlloperation.)The correct way to solve it is, as you mention, to do