亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Arrays.asList()使用注意點(diǎn)

原創(chuàng) 2016-11-07 13:47:14 372
摘要:今天看代碼時(shí), 發(fā)現(xiàn)書上使用了Arrays.asList()方法, 將一個(gè)數(shù)組轉(zhuǎn)成了List, 然后說到得到的List不能調(diào)用add(), remove()方法添加元素或者刪除,帶著疑問看了下內(nèi)部實(shí)現(xiàn)原理, 看了別人博客明白了, 為啥不能調(diào)用add(), remove()方法了.下面是轉(zhuǎn)載的一篇文章.Arrays工具類提供了一些比較實(shí)用的方法,比如sort, binarySearch, fill等

今天看代碼時(shí), 發(fā)現(xiàn)書上使用了Arrays.asList()方法, 將一個(gè)數(shù)組轉(zhuǎn)成了List, 然后說到得到的List不能調(diào)用add(), remove()方法添加元素或者刪除,帶著疑問看了下內(nèi)部實(shí)現(xiàn)原理, 看了別人博客明白了, 為啥不能調(diào)用add(), remove()方法了.下面是轉(zhuǎn)載的一篇文章.

Arrays工具類提供了一些比較實(shí)用的方法,比如sort, binarySearch, fill等。其中還有一個(gè)asList方法,此方法能夠?qū)⒁粋€(gè)變長參數(shù)或者數(shù)組轉(zhuǎn)換成List。
但是,這個(gè)生成的List,它是固定長度的,如果對(duì)其進(jìn)行add或者remove的操作,會(huì)拋出UnsupportedOperationException,為什么會(huì)這樣呢?
帶著疑問,查看一下Arrays的源碼,可以得到問題的結(jié)果。 

/**
     * 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 <tt>Collection.toArray</tt>.  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.
     * @see Collection#toArray()
     */
    public static <T> List<T> asList(T... a) {
  return new ArrayList<T>(a);
}

方法asList返回的是new ArrayList<T>(a)。但是,這個(gè)ArrayList并不是java.util.ArrayList,它是一個(gè)Arrays類中的重新定義的內(nèi)部類。


具體的實(shí)現(xiàn)如下: 

/**
     * @serial include
     */
    private static class ArrayList<E> extends AbstractList<E>
    implements RandomAccess, java.io.Serializable
    {
        private static final long serialVersionUID = -2764017481108945198L;
    private Object[] a;
    ArrayList(E[] array) {
            if (array==null)
                throw new NullPointerException();
        a = array;
    }
    public int size() {
        return a.length;
    }
    public Object[] toArray() {
        return (Object[])a.clone();
    }
    public E get(int index) {
        return (E)a[index];
    }
    public E set(int index, E element) {
        Object oldValue = a[index];
        a[index] = element;
        return (E)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;
        }
    }

從這個(gè)內(nèi)部類ArrayList的實(shí)現(xiàn)可以看出,它繼承了類AbstractList<E>,但是沒有重寫add和remove方法,沒有給出具體的實(shí)現(xiàn)。查看一下AbstractList類中對(duì)add和remove方法的定義,如果一個(gè)list不支持add和remove就會(huì)拋出UnsupportedOperationException。

public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {
    /**
     * Sole constructor.  (For invocation by subclass constructors, typically
     * implicit.)
     */
    protected AbstractList() {
}
/**
     * Appends the specified element to the end of this List (optional
     * operation). <p>
     *
     * This implementation calls <tt>add(size(), o)</tt>.<p>
     *
     * Note that this implementation throws an
     * <tt>UnsupportedOperationException</tt> unless <tt>add(int, Object)</tt>
     * is overridden.
     *
     * @param o element to be appended to this list.
     * 
     * @return <tt>true</tt> (as per the general contract of
     * <tt>Collection.add</tt>).
     * 
     * @throws UnsupportedOperationException if the <tt>add</tt> method is not
     *        supported by this Set.
     * 
     * @throws ClassCastException if the class of the specified element
     *        prevents it from being added to this set.
     * 
     * @throws IllegalArgumentException some aspect of this element prevents
     *            it from being added to this collection.
     */
    public boolean add(E o) {
    add(size(), o);
    return true;
    }
    /**
     * Inserts the specified element at the specified position in this list
     * (optional operation).  Shifts the element currently at that position
     * (if any) and any subsequent elements to the right (adds one to their
     * indices).<p>
     *
     * This implementation always throws an UnsupportedOperationException.
     *
     * @param index index at which the specified element is to be inserted.
     * @param element element to be inserted.
     * 
     * @throws UnsupportedOperationException if the <tt>add</tt> method is not
     *        supported by this list.
     * @throws ClassCastException if the class of the specified element
     *        prevents it from being added to this list.
     * @throws IllegalArgumentException if some aspect of the specified
     *        element prevents it from being added to this list.
     * @throws IndexOutOfBoundsException index is out of range (<tt>index <
     *        0 || index > size()</tt>).
     */
    public void add(int index, E element) {
    throw new UnsupportedOperationException();
    }
    /**
     * Removes the element at the specified position in this list (optional
     * operation).  Shifts any subsequent elements to the left (subtracts one
     * from their indices).  Returns the element that was removed from the
     * list.<p>
     *
     * This implementation always throws an
     * <tt>UnsupportedOperationException</tt>.
     *
     * @param index the index of the element to remove.
     * @return the element previously at the specified position.
     * 
     * @throws UnsupportedOperationException if the <tt>remove</tt> method is
     *        not supported by this list.
     * @throws IndexOutOfBoundsException if the specified index is out of
     *        range (<tt>index < 0 || index >= size()</tt>).
     */
    public E remove(int index) {
    throw new UnsupportedOperationException();
    }
}

至此,為什么Arrays.asList產(chǎn)生的List是不可添加或者刪除,否則會(huì)產(chǎn)生UnsupportedOperationException,就可以得到解釋了。

同時(shí)我們可以用List來接受Arrays.asList(array)返回的參數(shù)、原因是Arrays.asList(array)返回的雖然不是java.util.ArrayList但是返回的ArrayList同理也繼承自AbstractList

我們的AbstractList實(shí)現(xiàn)自List接口、所以可以用list接口來引用Arrays.asList(array);

如果我們想把一個(gè)變長或者數(shù)據(jù)轉(zhuǎn)變成List, 而且期望這個(gè)List能夠進(jìn)行add或者remove操作,那該怎么做呢? 

我們可以寫一個(gè)類似的方法,里面直接采用java.util.ArrayList即可。 

比如: 

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class MyArrays {
    public static <T> List<T> asList(T... a) {
        List<T> list = new ArrayList<T>();
        Collections.addAll(list, a);
        return list;
    }
}

測試代碼如下: 

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Test {
    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
        List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");
        print(stooges);
        List<List<String>> seasonsList = Arrays.asList(retrieveSeasonsList());
        print(seasonsList);
        /*
         * 自己實(shí)現(xiàn)一個(gè)asList方法,能夠添加和刪除。
         */
        List<String> list = MyArrays.asList("Larry", "Moe", "Curly");
        list.add("Hello");
        print(list);
    }
    private static <T> void print(List<T> list) {
        System.out.println(list);
    }
    private static List<String> retrieveSeasonsList() {
        List<String> seasonsList = new ArrayList<String>();
        seasonsList.add("Spring");
        seasonsList.add("Summer");
        seasonsList.add("Autumn");
        seasonsList.add("Winter");
        return seasonsList;
    }
}

輸出結(jié)果: 

[Larry, Moe, Curly] 
[[Spring, Summer, Autumn, Winter]] 
[Larry, Moe, Curly, Hello]


發(fā)佈手記

熱門詞條