慎用會致使UnsupportedOperationException的 Arrays.asList( )方法

因爲數組的大小不可變以及操做方法較小,對於同類型的多個對象,咱們習慣使用集合來存放它們並存放。javascript

經常使用的數組轉List 的方法 Arrays.asList(T... a );  傳遞多個可變參數,可轉換成ArrayList 對象java

1、案例,先來看兩個方法

    方法一:數組

public static void test1() {
        List<Integer> list = Arrays.asList(1, 2, 3, 4);
        list.set(1, 22);
        list.add(5);
        list.remove(1);
    }

方法二:dom

public static void test2() {
        List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 4));
        list.set(1, 22);
        list.add(5);
        list.remove(1);
    }

方法一中,list對象的add、remove操做都會操做失敗,拋出java.lang.UnsupportedOperationException (不支持的操做異常),而set方法能夠正常操做ide

方法二中,list對象的全部修改操做,均可以正常執行url

2、淺析

緣由是:Arrays.asList(1, 2, 3, 4) 返回的對象是 Arrays的內部類,繼承了AbstractList,AbstractList中的set, add, remove 抽象方法中都是直接throw new UnsupportedOperationException(); 但內部類 ArrayList 並未重寫父類的 add,remove方法。spa

因此,對Arrays.asList 在包裝一層,使用方法二的方式,生成的List對象就是咱們熟知的 ArrayList了code

3、深析

Arrays.asList 方法的源碼,注意英文文檔註釋的意思對象

/**
     * 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 <T> the class of the objects in the array
     * @param a the array by which the list will be backed
     * @return a list view of the specified array
     */
    @SafeVarargs
    @SuppressWarnings("varargs")
    public static <T> List<T> asList(T... a) {
        return new ArrayList<>(a);
    }

 

AbstractList的源碼:blog

/**
     * {@inheritDoc}
     *
     * <p>This implementation always throws an
     * {@code UnsupportedOperationException}.
     *
     * @throws UnsupportedOperationException {@inheritDoc}
     * @throws ClassCastException            {@inheritDoc}
     * @throws NullPointerException          {@inheritDoc}
     * @throws IllegalArgumentException      {@inheritDoc}
     * @throws IndexOutOfBoundsException     {@inheritDoc}
     */
    public E set(int index, E element) {
        throw new UnsupportedOperationException();
    }

 /**
     * {@inheritDoc}
     *
     * <p>This implementation always throws an
     * {@code UnsupportedOperationException}.
     *
     * @throws UnsupportedOperationException {@inheritDoc}
     * @throws ClassCastException            {@inheritDoc}
     * @throws NullPointerException          {@inheritDoc}
     * @throws IllegalArgumentException      {@inheritDoc}
     * @throws IndexOutOfBoundsException     {@inheritDoc}
     */
    public void add(int index, E element) {
        throw new UnsupportedOperationException();
    }

 /**
     * {@inheritDoc}
     *
     * <p>This implementation always throws an
     * {@code UnsupportedOperationException}.
     *
     * @throws UnsupportedOperationException {@inheritDoc}
     * @throws IndexOutOfBoundsException     {@inheritDoc}
     */
    public E remove(int index) {
        throw new UnsupportedOperationException();
    }
相關文章
相關標籤/搜索