Arrays.asList给出UnsupportedOperationException [英] Arrays.asList give UnsupportedOperationException

查看:108
本文介绍了Arrays.asList给出UnsupportedOperationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Arrays.asList 返回的 List 无法使用<$ c $等方法修改c>添加或删除。但是如果你将它传递给 Collections.sort 方法,它可以毫无问题地对数组进行排序(我预计会有例外)。这似乎是一种非常不一致的行为。那么 List 上的允许操作是什么,它由 asList 方法返回?

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.

修改:是的我能理解为什么它不支持删除添加。但是它怎么能支持排序?

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

推荐答案

Arrays.asList 返回固定大小列表由数组支持。因此,不支持删除添加。支持 set 。您可以查看此列表,就像它的行为完全类似于数组一样。数组具有固定长度。您不能添加或删除元素,但可以为数组的索引赋值,这相当于 List <的 set 方法/ code>。你可以对数组进行排序。

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)不会改变<的大小code>列表,因此可以对固定大小的列表进行排序。为了对 List 进行排序,您只需要交换 List 的元素。为此目的, set(index,element)就足够了。

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)

如果你看一下 Collections.sort ,你看到它实际上是对一个数组进行排序。它要求修改 List 的唯一 List 方法是 set 列表 ListIterator ,它调用列表's set(index,element)方法。

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]);
  }
}

这篇关于Arrays.asList给出UnsupportedOperationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆