本文主要是介绍Java中关于Arrays.asList方法的深入学习与理解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Java中关于Arrays.asList方法的深入学习与理解
Java的标准库中在java.util包下提供了很多实用的工具类,如:Arrays,Collections等工具类都提供了一些比较实用的方法。在实际的开发使用中,我们经常需要使用这样的需求:将一个数组转换为结合类(List等)或是将一个集合类转换为数组。
在java.util.Arrays中有一个 asList 方法,其作用是将一个数组转换为一个List集合对象。但是,我们调用这个方法生成的List,它是固定长度的,如果对其进行add或者remove的操作,jvm将会抛出 UnsupportedOperationException ,为什么会这样呢?
查看官方的API文档
@SafeVarargs
public static <T> List<T> asList(T... a)
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 Collection.toArray(). The returned list is serializable and implements RandomAccess.
This method also provides a convenient way to create a fixed-size list initialized to contain several elements:
List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");
Parameters:
a - the array by which the list will be backed
Returns:
a list view of the specified array
大概意思是:
返回一个大小固定的列表,该列表的长度决定于参数数组的长度。(对返回列表的更改操作会“直接反应”到参数数组上面。)此方法同 Collection.toArray() 一起,充当了基于数组的 API 与基于 collection 的 API 之间的桥梁。返回的列表是可序列化的,并且实现了 RandomAccess。
此方法还提供了一种创建固定长度的列表的便捷方法,该列表被初始化为包含所有方法参数的列表(不定长参数列表的使用):
List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");
从官方给出的API文档中我们只知道该列表大小不能变更,至于为什么不能像我们平时使用的List一样具有自扩展功能,参考文档上并没有明确的解释。要想要搞懂其中的原理,还真的‘深入虎穴’才行呀!没办法,直接查看源代码——开源就是好!!!
在JDK1.7中的源代码如下:
<span style="font-family:SimHei;"> </span><span style="font-family:Courier New;">@SafeVarargspublic static <T> List<T> asList(T... a) {return new ArrayList<>(a);}</span>
方法asList返回的是new ArrayList<T>(a)。按说应该具备List的方法呀,为什么会在使用add方法时抛异常呢?
莫着急,接着查看源代码时发现:这个ArrayList并不是java.util.ArrayList,它是一个Arrays类中的重新定义的内部类。
具体的实现如下:
<span style="font-family:Courier New;">/*** @serial include*/private static class ArrayList<E> extends AbstractList<E>implements RandomAccess, java.io.Serializable{private static final long serialVersionUID = -2764017481108945198L;private final E[] a;ArrayList(E[] array) {if (array==null)throw new NullPointerException();a = array;}public int size() {return a.length;}public Object[] toArray() {return a.clone();}public <T> T[] toArray(T[] a) {int size = size();if (a.length < size)return Arrays.copyOf(this.a, size,(Class<? extends T[]>) a.getClass());System.arraycopy(this.a, 0, a, 0, size);if (a.length > size)a[size] = null;return a;}public E get(int index) {return a[index];}public E set(int index, E element) {E oldValue = a[index];a[index] = element;return oldValue;}public int indexOf(Object o) {if (o==null) {for (int i=0; i<a.length; i++)if (a[i]==null)return i;} else {for (int i=0; i<a.length; i++)if (o.equals(a[i]))return i;}return -1;}public boolean contains(Object o) {return indexOf(o) != -1;}}</span>
从这个内部类ArrayList的实现我们可以发现,它继承自类AbstractList<E>,但是并没有重写父类中的add和remove方法,也就是没有给出自己的具体实现。
接下来我们继续查看一下AbstractList类中对add和remove方法的定义,如果一个list不支持add和remove就会抛出UnsupportedOperationException。
<span style="font-family:Courier New;">public boolean add(E e) {add(size(), e);return true;
}
public void add(int index, E element) {throw new UnsupportedOperationException();
}
public E remove(int index) {throw new UnsupportedOperationException();
}</span>
我相信看到这里大家就都明白了吧!
那么如果我们想把数组转变成List, 而且期望这个List能够进行add或者remove操作,那该怎么做呢?
如果还希望使用标准库中提供的工具类和方法,我们可以使用Collections类中的 addAll 方法。如果你有更多的精力与时间,也可以写一个自己的工具类,改变Arrays中的具体实现。
这篇关于Java中关于Arrays.asList方法的深入学习与理解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!