ArrayList的add方法报错: java.lang.UnsupportedOperationException

本文介绍了一个因使用Arrays.asList方法导致的UnsupportedOperationException异常案例,并详细解释了异常产生的原因及解决办法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天修改以前一个同事的代码,其中有一句是我新增的代码:

private static List<String> exceptionAPIs = new ArrayList<>();

//调用某些方法,处理数据

File file = new File(filePath);
try {
    BufferedReader reader = new BufferedReader(new FileReader(file));
    String line = reader.readLine();
    while (!StringUtils.isEmpty(line)) {
        list.add(line.trim());
        line = reader.readLine();
    }
} catch (IOException e) {
    e.printStackTrace();
}

结果却报了下面这个错误:

Caused by: java.lang.UnsupportedOperationException
    at java.util.AbstractList.add (AbstractList.java:148)
    at java.util.AbstractList.add (AbstractList.java:108)

从错误信息上看到,list的类型发生了变化,不在是java.util.ArrayList,而是java.util.AbstractList,找到这个类的add方法:

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

AbstractList是个抽象类,在执行我新写的代码之前,唯一的操作就是:

list = Arrays.asList(members.toArray(new String[]{}))

应该是这个方法的实现,改变了list的类型,找到这个方法:

/**
     * 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 <T> the class of the objects in the array
     * @param a the array by which the list will be backed
     * @return a list view of the specified array
     */
    @SafeVarargs
    @SuppressWarnings("varargs")
    public static <T> List<T> asList(T... a) {
        return new ArrayList<>(a);
    }

返回的数据看起来是ArrayList,找到这个ArrayList,在Arrays中,是个内部类:

private static class ArrayList<E> extends AbstractList<E>
        implements RandomAccess, java.io.Serializable

这个内部类实现上面的抽象类AbstractList,但是没有重写add方法,所以执行的还是抽象类中的add方法,而这个方法只会抛出异常 java.lang.UnsupportedOperationException,所以,解决这个问题只要把list的类型改回来就行了;
两种简单方式:

list = new ArrayList<>();
list = new ArrayList<>(list);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值