List 操作add 报错
操作List报java.lang.UnsupportedOperationException
问题描述


今天在项目中调用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 报错的更多相关文章
- 【java】在分页查询结果中对最后的结果集List进行操作add()或remove()操作,报错:java.lang.UnsupportedOperationException
场景: 在分页查询结果中对最后的结果集List进行操作add()或remove()操作,报错:java.lang.UnsupportedOperationException 错误: java.lang ...
- 关于使用CodeFirst,修改类或上下文时操作数据库报错解决方法
在操作已经创建好的数据库时,若是添加新的实体类或者修改原有数据库上下文,会报如下错误: The model backing the 'StudentDbContext' context has cha ...
- set调用add报错:
Map<String, String> goodsStandMap = new HashMap<>();goodsStandMap.put("key1", ...
- CentOS 7在执行yum操作时 报错
CentOS 7在执行yum操作时, 报错:Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=6&arch ...
- C#操作FTP报错,远程服务器返回错误:(550)文件不可用(例如,未找到文件,无法访问文件)的解决方法
最近在做项目的时候需要操作ftp进行文件的上传下载,但在调用using (var response = (FtpWebResponse)FtpWebRequest.GetResponse())的时候总 ...
- 使用使用for in 语句,并对数组中元素进行了增删操作,报错却不知怎么办?
解决方案: 在forin遍历过程中不要对遍历数据进行修改, for in 的时候如果在操作内移除会打乱 他的count 导致出错,如果要修改尽量用for循环
- java操作word报错及解决办法
Exception in thread "main" java.lang.UnsatisfiedLinkError: no jacob-1.17-x86 in java.libra ...
- C#操作Excel报错:服务器出现意外情况。
C#操作Excel表格时,如遇以下错误: 服务器出现意外情况.(异常来自 HRESULT:0x80010105(RPC_E_SERVERFAULT)) 解决方案: 打开你电脑中的Office-Exce ...
- 多线程下,多次操作数据库报错,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或者 ...
随机推荐
- Python数据分析 | Numpy与1维数组操作
作者:韩信子@ShowMeAI 教程地址:http://www.showmeai.tech/tutorials/33 本文地址:http://www.showmeai.tech/article-det ...
- oracle数据库表导出
寻找一个测试table,清空,将需要导出的表导入表中的任意一列,写导出语句,导出. EXPDP USERID='user/account@orcl11 as sysdba' schemas=temp ...
- Python中模块、类、函数、实例调用案例
19 a = '我是模块中的变量a' 20 21 def hi(): 22 a = '我是函数里的变量a' 23 print('函数"hi"已经运行!') 24 25 class ...
- 命令行下Git调用IDEA的diff功能
命令行下git diff, 有人欢喜有人厌, 本文以IDEA diff为例, 介绍如何更换Git的diff工具. IDEA diff IDEA作为一个图形化工具, 其实也提供了极少一部分命令行接口, ...
- jmeter参数化文件路径问题
问题 win下做好的带参数化文件的脚本,放到linux下运行,由于参数化文件路径不正确,导致脚本运行失败,如果解决这个问题呢? 方案一:参数化路径 比如,参数化文件我放到jmeter的bin目录下,参 ...
- BGP协议测试—信而泰网络测试仪实操
关键词 BGP; 协议仿真; 测试原理. 前言:当前信息化时代之下,数据传输已经成为了日常工作和生活必不可少的重要组成部分,网络服务的易得性和可靠性也因此得到广泛关注.这其中负责网络正常工作的诸多协 ...
- PyTorch 中的乘法:mul()、multiply()、matmul()、mm()、mv()、dot()
torch.mul() 函数功能:逐个对 input 和 other 中对应的元素相乘. 本操作支持广播,因此 input 和 other 均可以是张量或者数字. 举例如下: >>> ...
- $_SERVER["QUERY_STRING"],$_SERVER["REQUEST_URI"],$_SERVER["SCRIPT_NAME"] 和$_SERVER["PHP_SELF"]
$_SERVER["QUERY_STRING"],$_SERVER["REQUEST_URI"],$_SERVER["SCRIPT_NAME" ...
- VS常用的快捷键
整理代码 Ctrl+k+f 注释 Ctrl+k+c 取消注释 Ctrl+k+u 帮助文档 F1 无调试启动 ...
- MM32F0140 UART1中断接收和UART1中断发送
目录: 1.MM32F0140简介 2.初始化MM32F0140 UART1和NVIC中断 3.编写MM32F0140 UART1使能中断发送函数 4.编写MM32F0140 UART1中断接收和中断 ...