java

Java List.add() UnsupportedOperationException

八年前。

Java List.add() UnsupportedOperationException

______
Not every List implementation supports the add() method.
One common example is the List returned by Arrays.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 specific List you’re trying to modify, the answer still applies to other Listimplementations that are either immutable or only allow some selected changes.
You can find out about this by reading the documentation of UnsupportedOperationException and List.add(), which documents this to be an “(optional operation)”. The precise meaning of this phrase is explained at the top of the List documentation.
As a workaround you can create a copy of the list to a known-modifiable implementation like ArrayList:
seeAlso = new ArrayList<>(seeAlso);

https://stackoverflow.com/questions/5755477/java-list-add-unsupportedoperationexception

笔记

String-2 > sameStarChar 不会做

CodingBat
String-2 > sameStarChar

https://codingbat.com/prob/p194491

Returns true if for every ‘*’ (star) in the string, if there are chars both immediately before and after the star, they are the same.

如果string里面的每一个“*”,前面后面都有相同的char,就返回true?

public boolean sameStarChar(String str) {
for (int i=1;i<str.length()-1; i++){
if(str.charAt(i)==’*’ && String.valueOf(str.charAt(i-1)).equals(String.valueOf(str.charAt(i+1)))) {
return true;
}
}
return false;
}

所以这是什么意思??不懂。

笔记