java.lang.UnsupportedOperationException异常

写购物车单元测试时候,将某DTO对象放到List然后继续进行操作,向里面添加元素的时候报错了,代码逻辑如下

List<OrderDetail> orderDetailList = Arrays.asList();
        OrderDetail orderDetail= new OrderDetail();
        orderDetail.setProductId("1211");
        orderDetail.setProductQuantity(1);
        orderDetailList.add(orderDetail);

        orderDTO.setOrderDetails(orderDetailList);
        OrderDTO result =orderService.creat(orderDTO);
        log.info("【创建订单】 result={}",result);
报错如下,代码执行到add报错
java.lang.UnsupportedOperationException
	at java.util.AbstractList.add(AbstractList.java:148)
	at java.util.AbstractList.add(AbstractList.java:108)
难道Arrays.asList返回的List与new出来的有什么不同呢!根据报错的信息,是AbstractLst不支持add操作,从源码的结构看List的很多实现类都继承了AbstractList,包括常用的ArrayList。那么 Arrays.asList是怎么实现的呢,顺着源码追踪看下去,它的实现非常简单,如下:

/**
     * Returns a fixed-size list backed by the specified array.  (Changes to
     * the returned list "write through" to the array.)  This method acts
     * as bridge between array-based and collection-based APIs, in
     * combination with {@link Collection#toArray}.  The returned list is
     * serializable and implements {@link RandomAccess}.
     *
     * <p>This method also provides a convenient way to create a fixed-size
     * list initialized to contain several elements:
     * <pre>
     *     List&lt;String&gt; stooges = Arrays.asList("Larry", "Moe", "Curly");
     * </pre>
     *
     * @param a the array by which the list will be backed
     * @return a list view of the specified array
     */
    @SafeVarargs
    public static <T> List<T> asList(T... a) {
        return new ArrayList<>(a);
    }

      注释中倒是说明了返回的是个固定大小的List,确是用ArrayList实现的,当liuyh17211在追踪进ArrayList的时候真相大白了,原来这个ArrayList不是util包中的ArrayList,而只是Arrays类的一个继承了AbstractList内部类,所有代码如下:

/**
     * @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) {
            if (array==null)
                throw new NullPointerException();
            a = array;
        }

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

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

        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;
        }

        public E get(int index) {
            return a[index];
        }

        public E set(int index, E element) {
            E oldValue = a[index];
            a[index] = element;
            return oldValue;
        }

        public int indexOf(Object o) {
            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;
        }

        public boolean contains(Object o) {
            return indexOf(o) != -1;
        }
    }

    可以发现,这个类确实没有覆盖父类的实现,所以才报错,那还有哪些方法是不支持的呢,在AbstractList中,明确提到了不覆盖就会抛UnsupportedOperationException异常的方法有3个:add(int index, E element),set(int index, E element),remove(int index)。而上面的代码中只覆盖了set方法,可能会调用这几个方法的add(E element),clear(),addAll(int index, Collection<? extends E> c),甚至iterator()方法都没有覆盖,也就是说上面的几个方法都可能在调用中报错 由此可见JDK设计的这个返回List,只支持遍历和取值,不能做任何修改,只能作为传递值的桥梁。综上所述将代码改动如下 ,成功解决。

List<OrderDetail> orderDetailList = Arrays.asList();
改为
List<OrderDetail> orderDetailList = new ArrayList<>();


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值