Caused by: java.lang.UnsupportedOperationException

来源:互联网 发布:nfc读卡软件 编辑:程序博客网 时间:2024/06/05 09:13

对Arrays.asList()返回的List进行操作之后报错Caused by: java.lang.UnsupportedOperationException

让我们来看一下Arrays.asList的源码:

/**     * Returns a {@code List} of the objects in the specified array. The size of the     * {@code List} cannot be modified, i.e. adding and removing are unsupported, but     * the elements can be set. Setting an element modifies the underlying     * array.     *     * @param array     *            the array.     * @return a {@code List} of the elements of the specified array.     */    @SafeVarargs    public static <T> List<T> asList(T... array) {        return new ArrayList<T>(array);    }private static class ArrayList<E> extends AbstractList<E> implements            List<E>, Serializable, RandomAccess {        private static final long serialVersionUID = -2764017481108945198L;        private final E[] a;        ArrayList(E[] storage) {            if (storage == null) {                throw new NullPointerException("storage == null");            }            a = storage;        }        @Override        public boolean contains(Object object) {            if (object != null) {                for (E element : a) {                    if (object.equals(element)) {                        return true;                    }                }            } else {                for (E element : a) {                    if (element == null) {                        return true;                    }                }            }            return false;        }        @Override        public E get(int location) {            try {                return a[location];            } catch (ArrayIndexOutOfBoundsException e) {                throw java.util.ArrayList.throwIndexOutOfBoundsException(location, a.length);            }        }        @Override        public int indexOf(Object object) {            if (object != null) {                for (int i = 0; i < a.length; i++) {                    if (object.equals(a[i])) {                        return i;                    }                }            } else {                for (int i = 0; i < a.length; i++) {                    if (a[i] == null) {                        return i;                    }                }            }            return -1;        }        @Override        public int lastIndexOf(Object object) {            if (object != null) {                for (int i = a.length - 1; i >= 0; i--) {                    if (object.equals(a[i])) {                        return i;                    }                }            } else {                for (int i = a.length - 1; i >= 0; i--) {                    if (a[i] == null) {                        return i;                    }                }            }            return -1;        }        @Override        public E set(int location, E object) {            E result = a[location];            a[location] = object;            return result;        }        @Override        public int size() {            return a.length;        }        @Override        public Object[] toArray() {            return a.clone();        }        @Override        @SuppressWarnings({"unchecked", "SuspiciousSystemArraycopy"})        public <T> T[] toArray(T[] contents) {            int size = size();            if (size > contents.length) {                Class<?> ct = contents.getClass().getComponentType();                contents = (T[]) Array.newInstance(ct, size);            }            System.arraycopy(a, 0, contents, 0, size);            if (size < contents.length) {                contents[size] = null;            }            return contents;        }    }

索噶,原来返回的是Arrays类的静态内部类!这样能会报错也就正常咯。

0 0
原创粉丝点击