Arrays.asList的使用及异常问题
将数组转成List问题,通常我们习惯这样写成:List<String> list = Arrays.asList("1","2");
于是我们这样就得到了一个list,但是这个List的实现类是java.util.Arrays.ArrayList这个类(而不是java.util.ArrayList)。
剖析JDK源代码可以发现,java.util.Arrays.ArrayList(就是转换出来list)它是继承了java.util.AbstractList这个类。
再来看看java.util.AbstractList类是啥样子的?可以发现
public E set(int index, E element)
public E set(int index, E element)
public E remove(int index)
public void add(int index, E element)? | public boolean add(E e)调用add(int index, E element)
以上方法的实现全部是抛出UnsupportedOperationException异常。
因此有Arrays.asList转换出来的List他其实是一个AbstractList,他可以像List一样访问,但是不可其做任何修改操作。
这也说明了,为什么Arrays.asList出来的List,对其做add、remove操作为抛出UnsupportedOperationException异常,从JDK代码角度上,原因在此。
换句话说,其实java.util.Arrays.ArrayList其实只是对数组做了一个装饰,可以看到里面的实现,E get(int index)、E set(int index, E element)等方法都是对数组的操作,他的目的只是提供了可以像访问List那样来访问数组而已。本质上其实还是一个数组。
?
?
?
//?? 自己做个简单的例子
?
public class Te {
????? public static void main(String[] args){
??? ?? String[] in={"4","2","4","5","7"};
??? ??
??? ?? List isn=Arrays.asList(in);
//??? ?? 修改操作
??? ?? isn.set(1, "9");
??? ?? for(int i=0;i<isn.size();i++){
??? ??? System.out.println(isn.get(i));
??? ?? }
????? }
}
====================
JDK 1.4对java.util.Arrays.asList的定义,函数参数是Object[]。所以,在1.4中asList()并不支持基本类型的数组作参数。
?JDK 1.5中,java.util.Arrays.asList的定义,函数参数是Varargs, 采用了泛型实现。同时由于autoboxing的支持,使得可以支持对象数组以及基本类型数组。
?不过在使用时,当传入基本数据类型的数组时,会出现小问题,会把传入的数组整个当作返回的List中的第一个元素,例如:
view sourceprint?1
public
static
void
main(String[] args){
2
????
int
[] a1 =
new
int
[]{
1
,
2
,
3
};
3
????
String[] a2 =
new
String[]{
"a"
,
"b"
,
"c"
};
4
??????
?5
????
System.out.println(Arrays.asList(a1));
6
????
System.out.println(Arrays.asList(a2));
7
}
打印结果如下:
view sourceprint?1
[[I
@dc8569
]
2
[a, b, c]
下面说说Arrays.asList()的返回值:
JDK文档是这么说的:public static <T> List<T> asList(T... a) 返回一个受指定数组支持的固定大小的列表。(对返回列表的更改会“直接写”到数组。)此方法同 Collection.toArray() 一起,充当了基于数组的 API 与基于 collection 的 API 之间的桥梁。返回的列表是可序列化的,并且实现了 RandomAccess。此方法还提供了一个创建固定长度的列表的便捷方法,该列view sourceprint?01
/**
02
?
* @serial include
03
?
*/
04
?
private
static
class
ArrayList<E>
extends
AbstractList<E>
implements
RandomAccess, java.io.Serializable {
05
?????
private
static
final
long
serialVersionUID = -2764017481108945198L;
06
?????
private
final
E[] a;
07
??
?08
?????
ArrayList(E[] array) {
09
?????????
if
(array ==
null
)
10
?????????
throw
new
NullPointerException();
11
?????????
a = array;
12
?????
}
13
??????
?14
?????
public
int
size() {
15
????????
return
a.length;
16
?????
}
17
??????
?18
?????
public
Object[] toArray() {
19
????????
return
a.clone();
20
?????
}
21
??????
?22
?????
public
<T> T[] toArray(T[] a) {
23
?????????
int
size = size();
24
?????????
if
(a.length < size)
25
????????????
return
Arrays.copyOf(
this
.a, size, (Class<?
extends
T[]>) a.getClass());
26
?????????
System.arraycopy(
this
.a,
0
, a,
0
, size);
27
?????????
if
(a.length > size)
28
????????????
a[size] =
null
;
29
?????????
return
a;
30
?????
}
31
??????
?32
?????
public
E get(
int
index) {
33
????????
return
a[index];
34
?????
}
35
??????
?36
?????
public
E set(
int
index, E element) {
37
?????????
E oldValue = a[index];
38
?????????
a[index] = element;
39
?????????
return
oldValue;
40
?????
}
41
??????
?42
?????
public
int
indexOf(Object o) {
43
?????????
if
(o ==
null
) {
44
?????????????
for
(
int
i =
0
; i < a.length; i++)
45
?????????????
if
(a[i] ==
null
)
46
?????????????
return
i;
47
?????????
}
else
{
48
?????????????
for
(
int
i =
0
; i < a.length; i++)
49
?????????????
if
(o.equals(a[i]))
50
?????????????
return
i;
51
?????????
}
52
?????????
return
-
1
;
53
?????
}
54
??????
?55
?????
public
boolean
contains(Object o) {
56
????????
return
indexOf(o) != -
1
;
57
?????
}
58
}
表被初始化为包含多个元素: List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");
我们都知道,List的一个典型的特性就是其长度是可变的,我们可以很方便地对它进行插入和删除元素的操作,这是它与数组所存在的一个很大的区别,后者的长度是固定的,而且我们不能从数组中删除元素,只能修改元素的值。利用Arrays.asList(array)将返回一个List,然而这个返回的List并不支持add和remove的操作。
这是什么原因呢?
Arrays.asList源码:
view sourceprint?1
public
static
<T> List<T> asList(T... a) {
2
???
return
new
ArrayList<T>(a);
3
}
这里的ArrayList并不是java.util.ArrayList,而是Arrays的内部类:
我们可以看到该内部类继承的是AbstractList,下面是AbstractList的add和remove方法源码:
?
view sourceprint?01
public
boolean
add(E e) {
02
????
add(size(), e);
03
????
return
true
;
04
}
05
??
?06
public
void
add(
int
index, E element) {
07
????
throw
new
UnsupportedOperationException();
08
}
09
??
?10
public
E remove(
int
index) {
11
????
throw
new
UnsupportedOperationException();
12
}
?
所以,当我们对Arrays.asList返回的List进行添加或删除时将会报?java.lang.UnsupportedOperationException 异常。
=============================
?