Arrays.asList( ) 返回一個特殊的「ArrayList」

一個Arrays.asList()基本的用法:html

String[] str = new String[]{"1","2","3"};
ArrayList al = new ArrayList(Arrays.asList(str));//將數組元素添加到集合的一種快捷方式

有時候這樣使用就有可能出現問題:java

String[] str = new String[]{"1","2","3"};
List aslist = Arrays.asList(str);

aslist是List類型的對象,可是調用List接口的方法的時候會出錯就像這樣:數組

aslsit.add("4");
Exception in thread "main" java.lang.UnsupportedOperationException
	at java.util.AbstractList.add(Unknown Source)
	at java.util.AbstractList.add(Unknown Source)
	at test.LinkedListTest.main(LinkedListTest.java:13)

出現這個錯誤的緣由是沒有定義add()方法的具體實現,這些異常都在AbstractList拋出,看一下Arrays源碼:dom

@SafeVarargs
public static <T> List<T> asList(T... a) {
     return new ArrayList<>(a);
}

沒有問題啊,返回的是一個ArrayList,可是這個ArrayList不是ArrayList.class而是Arrays$ArrayList這樣一個內部類this

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

這個內部類中並無實現list的一些方法,因此調用add(),remove()這些方法都會出現錯誤。htm

這個內部類中有一個泛型的數組private final E[] a,因此Arrays.asList返回的集合中的數組實際上是有具體的類型的,而不是Object[]。對象

至關於List<String> list = new Arrays$ArrayList<String>();blog

可是有類型的數組在轉換過程當中就會出現某些問題,有個Bug就是這樣引發的。具體見 《c.toArray might not return Object[]》。  接口

相關文章
相關標籤/搜索