项目中执行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 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().getClass());
                        }
                        this.saveFinanceWithMdse(entity.getMdseCode(), fs);
                    }
                }
            }
        }

项目中执行ArrayList.remove() 抛出java.lang.UnsupportedOperationException_第1张图片

查询资料发现:
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}.
     *
     * 

This method also provides a convenient way to create a fixed-size * list initialized to contain several elements: *

     *     List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");
     * 
* * @param a the array by which the list will be backed * @return a list view of the specified array */ @SafeVarargs public static List 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[] 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;
        }

        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; iif (a[i]==null)
                        return i;
            } else {
                for (int i=0; iif (o.equals(a[i]))
                        return i;
            }
            return -1;
        }

        public boolean contains(Object o) {
            return indexOf(o) != -1;
        }
    }
    这是Arrays里的内部类,内部类实现的是AbstractList抽象类

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

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

你可能感兴趣的:(语法,java)