• Caused by: java.lang.UnsupportedOperationException


    对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类的静态内部类!这样能会报错也就正常咯。

  • 相关阅读:
    初学者之 Git 和 Github
    80端口与8080端口是两种不同的端口吗?他们到底有什么区别和联系?
    redis 基础(一) 初步了解redis
    spring 基础(四)浏览器跨域访问+拦截器(Interceptor)
    mysql商业版和社区版
    spring 基础(五) spring mvc RESTful
    解决idea控制台打印乱码问题
    springBoot 基础-拓展(二) 记录一些常用的配置文件
    SpringBoot 基础(零) SpringBoot和Spring
    springBoot 基础-拓展(一) spring-boot-starter
  • 原文地址:https://www.cnblogs.com/yangqiangyu/p/5143081.html
Copyright © 2020-2023  润新知