Arrays.asList 给出 UnsupportedOperationException [英] Arrays.asList give UnsupportedOperationException
问题描述
Arrays.asList
返回的List
不能通过add
或remove
等方法修改>.但是如果你将它传递给 Collections.sort
方法,它可以毫无问题地对数组进行排序(我预计会有一个例外).这似乎是一种非常不一致的行为.那么asList
方法返回的List
上允许的操作是什么?
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.
是的,我可以理解为什么它不支持 remove
或 add
.但是它怎么能支持排序呢?
Yes I can understand why it doesn't support remove
or add
. But then how can it support sorting?
推荐答案
Arrays.asList
返回由数组支持的固定大小的 List
.因此不支持 remove
和 add
.支持 set
.您可以将这个 List
视为它的行为与数组完全一样.数组具有固定长度.不能添加或删除元素,但可以为数组的索引赋值,相当于List
的set
方法.您可以对数组进行排序.
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)
不会改变 List
的大小,因此可以对固定大小的列表进行排序.为了对 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.
所有这些信息都可以在 Arrays
的 Javadoc 中找到:
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<String> 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
方法是List
的ListIterator<的
set
/code>,它调用 List
的 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屋!