Arrays.asList不能remove、add等,抛UnsupportedOperationException

来源:互联网 发布:sqoop导入数据到oracle 编辑:程序博客网 时间:2024/05/16 14:41

我们知道,Arrays.asList返回的是个ArrayList,但是为什么不能修改呢?而且,当我们这么操作的时候会抛UnsupportedOperationException异常

其实,原因有点隐蔽,当我们仔细看Arrays的源码会发现,Arrays自己实现了一个ArrayList,仅仅是名字和常用的那个相同而已,而它返回的数组对象可用的方法就在下边:

    @SafeVarargs    public static <T> List<T> asList(T... a) {        return new ArrayList<>(a);    }    /**     * @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;        }    }
而,题目所说的remove、add方法此实现类就没有实现,再看它的父类(AbstractList<E>)中关于这两个方法的是实现:

    /**     * {@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();    }

到这里,我们就已经很明朗的知道,Arrays.asList为什么不能移除、新增项了。

....................................................................

那么,我们该怎么办呢?

方法是有的,我们这么干:

String[] array = new String[]{"1","2","3","4"};List<String> list = new ArrayList<String>(Arrays.asList(array));
这样就可以对 list 进行add、remove操作了。




0 0
原创粉丝点击