SyntaxHighlighter.all(); java.lang.UnsupportedOperationException: null. (리스트 원소 삭제 안되는 문제) :: 게을러지고 싶어 부지런한 개발자

Arrays.asList()로 만들어서 그 중 원소 하나를 삭제하려 했더니 자꾸만 뜨는 java.lang.UnsupportedOperationException: null

아래처럼 하면 저 에러가 자꾸뜬다..

List<String> strList = Arrays.asList(new String[] {"A","B","C","D","E"});
strList.removeIf(data -> data.equals("A"));

 

어떻게 해결해야 할까? 찾아보니 Arrays.asList 로 생성한 리스트는 고정되어 있어 원소를 제거할 수 없다고 한다

그래서 아래와 같이 new ArrayList<>()  Arrays.asList 코드를 감싸서 리스트를 생성해야 리스트 원소를 삭제할 수 있다

List<String> strList = new ArrayList<>(Arrays.asList(new String[] {"A","B","C","D","E"}));
strList.removeIf(data -> data.equals("A"));

+ Recent posts