操作List报java.lang.UnsupportedOperationException

2018.03.12 16:52:01字数 230阅读 1683

问题描述

今天在项目中调用List的add(..)方法时,程序报了java.lang.UnsupportedOperationException,这个List并非是List list = new ArrayList()而来,而是用Arrays.asList(..)得到的:

List<String> list = Arrays.asList("test1", "test2", "test3");
list.add("test4");

运行结果:

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

原因分析

跟进到asList(..)方法的源码中:

public static <T> List<T> asList(T... a) {
return new ArrayList<>(a);
}

坑就在这里,它返回的这个ArrayList并不是java.util.ArrayList<E>,而是java.util.Arrays的内部类。

 

这两个ArrayList都继承自AbstractList<E>,跟进到AbstractList<E>里面看源码:

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

可以看出父类中的set,add,remove方法都是直接抛出UnsupportedOperationException,再回到Arrays的内部类ArrayList,并没有去重写上述方法,因此使用List.asList(..)返回的Arrays内部类ArrayList对象进行add等操作时会抛此异常。再看java.util.ArrayList<E>中,都是对上诉方法进行了重写,因此通过new ArrayList()得到的List进行add等操作,不会有问题。


解决方法

很简单:

List<String> list = new ArrayList<>(Arrays.asList("test1", "test2", "test3"));
list.add("test4");

List 操作add 报错的更多相关文章

  1. 【java】在分页查询结果中对最后的结果集List进行操作add()或remove()操作,报错:java.lang.UnsupportedOperationException

    场景: 在分页查询结果中对最后的结果集List进行操作add()或remove()操作,报错:java.lang.UnsupportedOperationException 错误: java.lang ...

  2. 关于使用CodeFirst,修改类或上下文时操作数据库报错解决方法

    在操作已经创建好的数据库时,若是添加新的实体类或者修改原有数据库上下文,会报如下错误: The model backing the 'StudentDbContext' context has cha ...

  3. set调用add报错:

    Map<String, String> goodsStandMap = new HashMap<>();goodsStandMap.put("key1", ...

  4. CentOS 7在执行yum操作时 报错

    CentOS 7在执行yum操作时, 报错:Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=6&arch ...

  5. C#操作FTP报错,远程服务器返回错误:(550)文件不可用(例如,未找到文件,无法访问文件)的解决方法

    最近在做项目的时候需要操作ftp进行文件的上传下载,但在调用using (var response = (FtpWebResponse)FtpWebRequest.GetResponse())的时候总 ...

  6. 使用使用for in 语句,并对数组中元素进行了增删操作,报错却不知怎么办?

    解决方案: 在forin遍历过程中不要对遍历数据进行修改, for in 的时候如果在操作内移除会打乱 他的count 导致出错,如果要修改尽量用for循环

  7. java操作word报错及解决办法

    Exception in thread "main" java.lang.UnsatisfiedLinkError: no jacob-1.17-x86 in java.libra ...

  8. C#操作Excel报错:服务器出现意外情况。

    C#操作Excel表格时,如遇以下错误: 服务器出现意外情况.(异常来自 HRESULT:0x80010105(RPC_E_SERVERFAULT)) 解决方案: 打开你电脑中的Office-Exce ...

  9. 多线程下,多次操作数据库报错,There is already an open DataReader associated with this Command which must be closed first.

    原文:https://www.cnblogs.com/sdusrz/p/4433108.html 执行SqlDataReader.Read之后,如果还想用另一个SqlCommand执行Insert或者 ...

随机推荐

  1. Vue2.0源码学习(2) - 数据和模板的渲染(下)

    vm._render是怎么实现的 上述updateComponent方法调用是运行了一个函数: // src\core\instance\lifecycle.js updateComponent = ...

  2. 简述对CT,IT,ICT,OT的认识

    今天碰到一个关键词:CT.CT领域,所以给自己做一个科普. 网络:简述对CT,IT,ICT,OT的认识 一.通信技术-CT(Communication Technology) 最早的CT业被称为电信业 ...

  3. systemd配置文件填写了ExecStop=/usr/bin/kill -9 $MAINPID之后重启在messages发生了报错

    原因在于systemd模块需要增加自动化检测,检测有一项为检测messages日志内是否有systemd的failed 写了一个检测脚本,脚本的检测messages内容为/bin/cat /var/l ...

  4. 命令行与C++

    windows和Linux都接受使用命令行进行程序的运行,例如在windows下使用命令行命令.\notepad可以打开记事本,使用特定的命令行参数,如.\notepad 1.txt可以打开1.txt ...

  5. Java笔记——循环语句

    Java笔记--循环语句     1. while语句 规律: 1. 首先计算表达式的值. 2. 若表达式为真,则执行循环语法,直至表达式为假,循环结束.   while(表达式) 语句; 例如: i ...

  6. WPF中TreeView控件SelectedItemChanged方法的MVVM绑定

    问题描述:左侧treeview控件中点击不同类别的节点时,右侧的页面会显示不同的权限.比如对于My Publications,拥有Modify和Delete两种权限,对于My Subscription ...

  7. Weblogic补丁升级常见问题

    转至:https://blog.csdn.net/weixin_44659716/article/details/106804177 常用企业级版本:Weblogic 11g(weblogic10.3 ...

  8. Java课程设计---数据库工具类

    接下来看看传统的查询方式(一个完整的查询) package com.java.mysql; import java.sql.Connection; import java.sql.DriverMana ...

  9. POJ2112题解

    题目大意:K个挤奶机,C头牛,每个挤奶机最多可以接待M头牛,各个K,C之间可能有道路连接,要让每个牛都找到挤奶机,求最小的走的路程最远的牛所需走的距离. 思路:首先看到要最小化最大值,所以需要二分.可 ...

  10. P1424

    #include <stdio.h> int main(){ int s = 250; int x, n, distance = 0; scanf("%d %d",&a ...