Why do I get an UnsupportedOperationException when trying to remove an element from a List?

后端 未结 17 2010
后悔当初
后悔当初 2020-11-22 07:43

I have this code:

public static String SelectRandomFromTemplate(String template,int count) {
   String[] split = template.split(\"|\");
   List         


        
17条回答
  •  一生所求
    2020-11-22 07:52

    This one has burned me many times. Arrays.asList creates an unmodifiable list. From the Javadoc: Returns a fixed-size list backed by the specified array.

    Create a new list with the same content:

    newList.addAll(Arrays.asList(newArray));
    

    This will create a little extra garbage, but you will be able to mutate it.

提交回复
热议问题