Arrays.asList give UnsupportedOperationException [duplicate]

做~自己de王妃 提交于 2019-12-17 14:50:10

问题


The List returned by Arrays.asList can't be modified by using methods such as add or remove. But if you pass it to the Collections.sort method, it can sort the array without any problem (I expected an exception). This seems like a very inconsistent behaviour. So what are the allowed operations on the List, which returned by asList method?

List<Integer> list = Arrays.asList(5,7, 10 , 8,9);
list.remove(2);//Exception

Collections.sort(list);//Ok, No Exception Sort...
System.out.println(list);

I couldn't find any clue for this in the documentation.

Edit: Yes I can understand why it doesn't support remove or add. But then how can it support sorting?


回答1:


Arrays.asList returns a fixed size List backed by an array. Therefore remove and add are not supported. set is supported. You can look at this List as if it behaves exactly like an array. An array has a fixed length. You can't add or remove elements, but you can assign values to the indices of the array, which is equivalent to the set method of List. And you can sort an array.

Collections.sort(list) doesn't change the size of the List, so is can sort a fixed size list. All you need in order to sort a List is to swap elements of the List. For this purpose set(index,element) is sufficient.

All this information is found in the Javadoc of Arrays :

/**
 * Returns a fixed-size list backed by the specified array.  (Changes to
 * the returned list "write through" to the array.)  This method acts
 * as bridge between array-based and collection-based APIs, in
 * combination with {@link Collection#toArray}.  The returned list is
 * serializable and implements {@link RandomAccess}.
 *
 * <p>This method also provides a convenient way to create a fixed-size
 * list initialized to contain several elements:
 * <pre>
 *     List&lt;String&gt; stooges = Arrays.asList("Larry", "Moe", "Curly");
 * </pre>
 *
 * @param a the array by which the list will be backed
 * @return a list view of the specified array
 */
 public static <T> List<T> asList(T... a)

And if you look at an implementation of Collections.sort, you see that it actually sorts an array. The only List method it requires that modified the List is set of the List's ListIterator, which calls the List's set(index,element) method.

public static <T extends Comparable<? super T>> void sort(List<T> list) {
  Object[] a = list.toArray();
  Arrays.sort(a);
  ListIterator<T> i = list.listIterator();
  for (int j=0; j<a.length; j++) {
      i.next();
      i.set((T)a[j]);
  }
}



回答2:


Arrays.asList gives you a List backed by the array you give it. Arrays are fixed size. The wrapping List supports the same operation that arrays do, so you can reassign the entries, but you can't change its length.



来源:https://stackoverflow.com/questions/30174623/arrays-aslist-give-unsupportedoperationexception

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!