IT/java
Arrays.asList ... clear remove, UnsupportedOperationException ...
iezs
2017. 9. 14. 14:43
from https://stackoverflow.com/questions/7885573/remove-on-list-created-by-arrays-aslist-throws-unsupportedoperationexception
List 를 한번에 생성할때 간단히 Arryas.asList("a","b"...) 형태로 만들 수 있다.
간단하고 좋긴한데...
해당 리스트를 clear() remove 할때 에러가 난다. 내용은 UnsupportedOperationException
내용을 확인해보니 asList 로 만들어진 결과물은 fixed size 여서 말 그대로 지원하지 않는 동작이 되어버리는것이다.
해서 이 부분을 해결하고 싶다면
그냥 Arrays.asList 하지 말고
List<String> friends = new ArrayList<>(Arrays.asList("tom","haddy","duck","gates","stranger"));
형태로 ArrayList 의 생성자로 전달해서 만들어지면 clear 등도 잘 지원하게 된다.