项目中执行ArrayList.remove() 抛出java.lang.UnsupportedOperationException

项目中代码:

if(attrEntity!=null && attrEntity.size()>0){

            mdseCodes = new ArrayList<String>();
            for(MdseAttributeVO entity:attrEntity){
                if(StringUtils.isNotBlank(entity.getAttrValue())){
                    String str[] = entity.getAttrValue().split(",");
                    List<String> list = Arrays.asList(str);
                    List<FinanceSolution> fs =null;
                    if(list.contains(String.valueOf(solutionId))){
                        if(list.size()==1){
                            mdseCodes.add(entity.getMdseCode());
                        }else{
                        //这部抛出异常
                            list.remove(String.valueOf(solutionId));
                            fs = JSONObject.parseObject(JSON.toJSONString(list),new ArrayList<FinanceSolution>().getClass());
                        }
                        this.saveFinanceWithMdse(entity.getMdseCode(), fs);
                    }
                }
            }
        }

这里写图片描述

查询资料发现:
String str[] = {“1”,”2”,”3”}
List list = Arrays.asList(str);
这样的list是Arrays里的内部类,即Arrays$ArrayList,默认是不能执行remove、add操作的,操作会抛出异常

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

其中return 的new ArrayList<>(a)源码是:

    /**
     * @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;
        }
    }
    这是Arrays里的内部类,内部类实现的是AbstractList<E>抽象类

AbstractList抽象类源码:

    /**
     * {@inheritDoc}
     *
     * <p>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();
    }

    /**
     * {@inheritDoc}
     *
     * <p>This implementation always throws an
     * {@code UnsupportedOperationException}.
     *
     * @throws UnsupportedOperationException {@inheritDoc}
     * @throws IndexOutOfBoundsException     {@inheritDoc}
     */
    public E remove(int index) {
        throw new UnsupportedOperationException();
    }
    看到没,执行add、remove抛出UnsupportedOperationException异常

解决办法
String str[] = {“1”,”2”,”3”}
List list = Arrays.asList(str);
//把list的值迭代到ArrayList中就可以了
List arrayList = new ArrayList(list );

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值