八年前。
Java List.add() UnsupportedOperationException
______
Not everyList
implementation supports theadd()
method.
One common example is theList
returned byArrays.asList()
: it is documented not to support any structural modification (i.e. removing or adding elements) (emphasis mine):
Returns a fixed-size list backed by the specified array.
Even if that’s not the specificList
you’re trying to modify, the answer still applies to otherList
implementations that are either immutable or only allow some selected changes.
You can find out about this by reading the documentation ofUnsupportedOperationException
andList.add()
, which documents this to be an “(optional operation)”. The precise meaning of this phrase is explained at the top of theList
documentation.
As a workaround you can create a copy of the list to a known-modifiable implementation likeArrayList
:seeAlso = new ArrayList<>(seeAlso);
https://stackoverflow.com/questions/5755477/java-list-add-unsupportedoperationexception