java.util.Arrays.asList when used with removeIf throws UnsupportedOperationException

后端 未结 1 648

I am preparing for an OCPJP 8 exam for the next 2 months and currently I this one got my attention as I dont understand why

public class BiPredicateTest {
           


        
相关标签:
1条回答
  • 2020-12-31 03:55

    java.util.Arrays.asList() produces a list from which it is impossible to remove elements, so it throws on a removal attempt.

    You could wrap it with ArrayList:

    List<Integer> ints = new java.util.ArrayList<>(java.util.Arrays.asList(1,20,20));
    

    Update

    Arrays.asList() returns return new ArrayList<>(a); where ArrayList is not java.util.ArrayList, but java.util.Arrays.ArrayList (internal class), which does not allow removal.

    0 讨论(0)
提交回复
热议问题