数组通过Arrays.asList()方法转换成ArrayList后,使用add()和remove()抛UnsupportedOperationException异常

当把数组通过Arrays.asList()方法转换成ArrayList时,是转换成了java.util.Arrays类的内部类ArrayList,而不是java.util.ArrayList,虽然这两个类都继承了java.util.AbstractList类,但java.util.Arrays类的内部类ArrayList并没有重写add()和remove()方法,此时调用add()和remove()方法将直接调用AbstractList默认的add()和remove()方法,而直接抛UnsupportedOperationException异常。
若想正常使用add和remove方法,需先转换成ArryList

List<String> list = new ArrayList<>(Arrays.asList(new String[]{
     "a","b"});

java.util.AbstractList类:

public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {
     
	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();
    }
}

java.util.Arrays类的内部类ArrayList:

	@SuppressWarnings("varargs")
    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) {
     
            a = Objects.requireNonNull(array);
        }

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

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

        @Override
        @SuppressWarnings("unchecked")
        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;
        }

        @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<E> spliterator() {
     
            return Spliterators.spliterator(a, Spliterator.ORDERED);
        }

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

        @Override
        public void replaceAll(UnaryOperator<E> 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<? super E> c) {
     
            Arrays.sort(a, c);
        }
    }

java.util.ArrayList类:

public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable{
     
	public boolean add(E e) {
     
        ensureCapacityInternal(size + 1);  // Increments modCount!!
        elementData[size++] = e;
        return true;
    }


    public void add(int index, E element) {
     
        rangeCheckForAdd(index);

        ensureCapacityInternal(size + 1);  // Increments modCount!!
        System.arraycopy(elementData, index, elementData, index + 1,
                         size - index);
        elementData[index] = element;
        size++;
    }

    public E remove(int index) {
     
        rangeCheck(index);

        modCount++;
        E oldValue = elementData(index);

        int numMoved = size - index - 1;
        if (numMoved > 0)
            System.arraycopy(elementData, index+1, elementData, index,
                             numMoved);
        elementData[--size] = null; // clear to let GC do its work

        return oldValue;
    }

    public boolean remove(Object o) {
     
        if (o == null) {
     
            for (int index = 0; index < size; index++)
                if (elementData[index] == null) {
     
                    fastRemove(index);
                    return true;
                }
        } else {
     
            for (int index = 0; index < size; index++)
                if (o.equals(elementData[index])) {
     
                    fastRemove(index);
                    return true;
                }
        }
        return false;
    }
}

你可能感兴趣的:(用法,Arrays,List,ArrayList)