Arrays.asList的坑

碰到这样一个问题使用Arrays.asList返回的ArrayList无法进行add,remove操作,测试一下原因
原来在调用Arrays.asList的时候

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

但是这里的ArrayList并不是java.util.ArrayList而是java.util.Arrays$ArrayList
是Arrays的内部静态类,下面是内部类的定义

private static class ArrayList extends AbstractList
        implements RandomAccess, java.io.Serializable
    {
        private static final long serialVersionUID = -2764017481108945198L;
        private final E[] a;

        ArrayList(E[] array) {
            a = Objects.requireNonNull(array);
        }

        @Override
        public int size() {
            return a.length;
        }

        @Override
        public Object[] toArray() {
            return a.clone();
        }

        @Override
        @SuppressWarnings("unchecked")
        public  T[] toArray(T[] a) {
            int size = size();
            if (a.length < size)
                return Arrays.copyOf(this.a, size,
                                     (Class) a.getClass());
            System.arraycopy(this.a, 0, a, 0, size);
            if (a.length > size)
                a[size] = null;
            return a;
        }

        @Override
        public E get(int index) {
            return a[index];
        }

        @Override
        public E set(int index, E element) {
            E oldValue = a[index];
            a[index] = element;
            return oldValue;
        }

        @Override
        public int indexOf(Object o) {
            E[] a = this.a;
            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;
        }

        @Override
        public boolean contains(Object o) {
            return indexOf(o) != -1;
        }

        @Override
        public Spliterator spliterator() {
            return Spliterators.spliterator(a, Spliterator.ORDERED);
        }

        @Override
        public void forEach(Consumer action) {
            Objects.requireNonNull(action);
            for (E e : a) {
                action.accept(e);
            }
        }

        @Override
        public void replaceAll(UnaryOperator operator) {
            Objects.requireNonNull(operator);
            E[] a = this.a;
            for (int i = 0; i < a.length; i++) {
                a[i] = operator.apply(a[i]);
            }
        }

        @Override
        public void sort(Comparator c) {
            Arrays.sort(a, c);
        }
    }

可见他没有定义add和remove方法,而在外部调用他的add和remove方法时,使用的是抽象父类 AbstractList类中的
add和remove方法
AbstractList

public void add(int index, E element) {
        throw new UnsupportedOperationException();
    }
public E remove(int index) {
        throw new UnsupportedOperationException();
    }

可见如果子类没有重写这两个方法,这个子类是无法添加删除元素的。
解决方法:
List x = new ArrayList<>(Arrays.asList(1,2,3))

你可能感兴趣的:(Arrays.asList的坑)