写购物车单元测试时候,将某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<String> 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<>();