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

后端 未结 17 1964
后悔当初
后悔当初 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 08:06

    Yes, on Arrays.asList, returning a fixed-size list.

    Other than using a linked list, simply use addAll method list.

    Example:

    String idList = "123,222,333,444";
    
    List<String> parentRecepeIdList = new ArrayList<String>();
    
    parentRecepeIdList.addAll(Arrays.asList(idList.split(","))); 
    
    parentRecepeIdList.add("555");
    
    0 讨论(0)
  • 2020-11-22 08:08

    Replace

    List<String> list=Arrays.asList(split);
    

    to

    List<String> list = New ArrayList<>();
    list.addAll(Arrays.asList(split));
    

    or

    List<String> list = new ArrayList<>(Arrays.asList(split));
    

    or

    List<String> list = new ArrayList<String>(Arrays.asList(split));
    

    or (Better for Remove elements)

    List<String> list = new LinkedList<>(Arrays.asList(split));
    
    0 讨论(0)
  • 2020-11-22 08:10

    Just read the JavaDoc for the asList method:

    Returns a {@code List} of the objects in the specified array. The size of the {@code List} cannot be modified, i.e. adding and removing are unsupported, but the elements can be set. Setting an element modifies the underlying array.

    This is from Java 6 but it looks like it is the same for the android java.

    EDIT

    The type of the resulting list is Arrays.ArrayList, which is a private class inside Arrays.class. Practically speaking, it is nothing but a List-view on the array that you've passed with Arrays.asList. With a consequence: if you change the array, the list is changed too. And because an array is not resizeable, remove and add operation must be unsupported.

    0 讨论(0)
  • 2020-11-22 08:10

    Arrays.asList() uses fixed size array internally.
    You can't dynamically add or remove from thisArrays.asList()

    Use this

    Arraylist<String> narraylist=new ArrayList(Arrays.asList());
    

    In narraylist you can easily add or remove items.

    0 讨论(0)
  • 2020-11-22 08:14

    You can't remove, nor can you add to a fixed-size-list of Arrays.

    But you can create your sublist from that list.

    list = list.subList(0, list.size() - (list.size() - count));

    public static String SelectRandomFromTemplate(String template, int count) {
       String[] split = template.split("\\|");
       List<String> list = Arrays.asList(split);
       Random r = new Random();
       while( list.size() > count ) {
          list = list.subList(0, list.size() - (list.size() - count));
       }
       return StringUtils.join(list, ", ");
    }
    

    *Other way is

    ArrayList<String> al = new ArrayList<String>(Arrays.asList(template));
    

    this will create ArrayList which is not fixed size like Arrays.asList

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