Arrays.asList创建的数组 add元素抛错 java.lang.UnsupportedOperationException

List testArray= Arrays.asList(1,2,3);

testArray.add(5); // 此行代码会抛出异常

通过源码可知,该方法产生一个内部类的ArrayList对象,而不是java.utils.ArrayList对象。

两个ArrayList都继承了AbstractList类。但是内部类的ArrayList没有重写add和remove的方法。

如果内部类ArrayList对象直接调用add()或remove()则会抛出不支持的操作异常“java.lang.UnsupportedOperationException”

内部类源码

内部没有add和remove的方法,当该对象调用add和remove实际上是调用了AbstractList类中的add和remove。

   /**
     * @serial include  
     */
    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);
        }
    }

AbstractList的add()和remove()源码

public abstract class AbstractList extends AbstractCollection implements List {
    
    protected AbstractList() {
    }


    public boolean add(E e) {
        add(size(), e);
        return true;
    }
   
    public void add(int index, E element) {
        throw new UnsupportedOperationException();
    }

    public E remove(int index) {
        throw new UnsupportedOperationException();
    }

}

 

你可能感兴趣的:(Exception,exception,java)