java.lang.UnsupportedOperationException问题

 1、异常代码:

public static void main(String[] args) {
    String[] arr = new String[]{"a", "b", "c"};
    //在使用Arrays.asList转化数组成为list的时候,如果做添加操作,不能直接Arrays.asList()
    List<String> list = Arrays.asList(arr); 
    list.add("e"); //添加元素"e", 报java.lang.UnsupportedOperationException
}

2、异常信息:

Exception in thread "main" java.lang.UnsupportedOperationException
	at java.util.AbstractList.add(AbstractList.java:148)
	at java.util.AbstractList.add(AbstractList.java:108)

3、异常分析:

在使用 Arrays.asList()转化数组成为list的时候,生成了ArrayList, 表面上看是 java.util.ArrayList,实际上是Arrays的内部类ArrayList, 两个ArrayList,都是继承 AbstractList,这他妈就是坑,不进去看源码仔细了解,还以为是一个ArrayList,但是进去后才发现,Arrays的内部类ArrayList没有重写 AbstractList的add和remove方法,再去看AbstractList的add和remove方法,居然是直接抛出异常 java.lang.UnsupportedOperationException,没有任何处理。这就导致上面这种方法生成的Arrays的ArrayList使用add或者remove方法会直接抛出异常。

public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {
    /**
     * Sole constructor.  (For invocation by subclass constructors, typically
     * implicit.)
     */
    protected AbstractList() {
    }

    /**
     * Appends the specified element to the end of this list (optional
     * operation).
     *
     * <p>Lists that support this operation may place limitations on what
     * elements may be added to this list.  In particular, some
     * lists will refuse to add null elements, and others will impose
     * restrictions on the type of elements that may be added.  List
     * classes should clearly specify in their documentation any restrictions
     * on what elements may be added.
     *
     * <p>This implementation calls {@code add(size(), e)}.
     *
     * <p>Note that this implementation throws an
     * {@code UnsupportedOperationException} unless
     * {@link #add(int, Object) add(int, E)} is overridden.
     *
     * @param e element to be appended to this list
     * @return {@code true} (as specified by {@link Collection#add})
     * @throws UnsupportedOperationException if the {@code add} operation
     *         is not supported by this list
     * @throws ClassCastException if the class of the specified element
     *         prevents it from being added to this list
     * @throws NullPointerException if the specified element is null and this
     *         list does not permit null elements
     * @throws IllegalArgumentException if some property of this element
     *         prevents it from being added to this list
     */
    public boolean add(E e) {
        add(size(), e);
        return true;
    }

    /**
     * {@inheritDoc}
     *
     * @throws IndexOutOfBoundsException {@inheritDoc}
     */
    abstract public E get(int index);

    /**
     * {@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 E set(int index, E element) {
        throw new UnsupportedOperationException();
    }

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

    ........
    ........
    ........
}

4、解决方法:

在使用Arrays.asList()做转化的时候,如果要进行修改操作,需要再转化一次。

public static void main(String[] args) {
    String[] arr = new String[]{"a", "b", "c"};
    //在使用Arrays.asList转化数组成为list的时候,如果做添加操作,不能这样用
    //List<String> list = Arrays.asList(arr); 
    //list.add("e"); //添加元素"e", 报java.lang.UnsupportedOperationException
		
    //在使用Arrays.asList()做转化的时候,如果要进行修改操作,需要再转化一次。
    List<String> list = new ArrayList<>(Arrays.asList(arr));
    list.add("e");
    System.out.println(list);
}

使用new ArrayList<>这样再转化为 java.util.ArrayList ,就可以进行修改操作了。

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值