java.lang.UnsupportedOperationException at java.util.AbstractList.remove(Unknown Source)

前端 未结 5 1910
北荒
北荒 2021-02-02 06:03

I have tried below code

String s[]={\"1\",\"2\",\"3\",\"4\"};  
Collection c=Arrays.asList(s);  
System.out.println(c.remove(\"1\") +\"  remove flag\");  

Syste         


        
5条回答
  •  别跟我提以往
    2021-02-02 06:08

    The List returned by Arrays.asList method of java.util.Arrays class is a fixed-size list object which means that elements cannot be added to or removed from the list.

    So functions like Adding or Removing cannot be operated on such kind of Lists.

    The solution to adding or removing without getting java.lang.UnsupportedOperationException is ->

    List strList= new ArrayList<>(Arrays.asList(strs));
    
    //Then Add or Remove can be called on such List
    
    newList.add("100");
    newList.remove("100");
    
    

提交回复
热议问题