菜鸟笔记
提升您的技术认知

java.lang.unsupportedoperationexception解决方法-ag真人游戏

在项目中对list进行操作时报错java.lang.unsupportedoperationexception,后来发现操作的list是由数组转换而成的,通过看源码发现问题,并写测试程序如下。 
代码块:

public class listtest {
    public static void main(string[] args) {
        string[] array = {"1","2","3","4","5"};
        list list = arrays.aslist(array);
        list.add("6");
    }
}

执行结果:

exception in thread "main" java.lang.unsupportedoperationexception
    at java.util.abstractlist.add(abstractlist.java:148)
    at java.util.abstractlist.add(abstractlist.java:108)
    at com.atguigu.test.listtest.main(listtest.java:11)
    at sun.reflect.nativemethodaccessorimpl.invoke0(native method)
    at sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62)
    at sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43)
    at java.lang.reflect.method.invoke(method.java:498)
    at com.intellij.rt.execution.application.appmain.main(appmain.java:144)

发生问题的原因如下: 
调用arrays.aslist()生产的list的add、remove方法时报异常,这是由arrays.aslist() 返回的是arrays的内部类arraylist, 而不是java.util.arraylist。arrays的内部类arraylist和java.util.arraylist都是继承abstractlist,remove、add等方法abstractlist中是默认throw unsupportedoperationexception而且不作任何操作。java.util.arraylist重新了这些方法而arrays的内部类arraylist没有重新,所以会抛出异常。解决方法如下:

public class listtest {
    public static void main(string[] args) {
        string[] array = {"1","2","3","4","5"};
        list list = arrays.aslist(array);
        list arrlist = new arraylist(list);
        arrlist.add("6");
    }
}

 

arrays源码:

   public static  list aslist(t... a) {
        return new arraylist<>(a);
    }
    /**
     * @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源码:
 /**
     * {@inheritdoc}
     *
     * 

this implementation always throws an * {@code unsupportedoperationexception}. * * @throws unsupportedoperationexception {@inheritdoc} * @throws classcastexception {@inheritdoc} * @throws nullpointerexception {@inheritdoc} * @throws illegalargumentexception {@inheritdoc} * @throws indexoutofboundsexception {@inheritdoc} */ public void add(int index, e element) { throw new unsupportedoperationexception(); }

网站地图