操作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. 终极指南:企业级云原生 PaaS 平台日志分析架构全面解析

    早些时候 Erda Show 针对微服务监控.日志等内容做了专场分享,很多同学听完后意犹未尽,想了解更多关于日志分析的内容.Erda 团队做日志分析也有一段时间了,所以这次打算和大家详细分享一下我们在 ...

  2. Python_time库_特定字符串格式的时间、struct_time、时间戳的处理

    time库 时间戳:格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数. # time.strptime(),功能:将特定字符串格 ...

  3. 【笔试必备】常见sql笔试题(30题)

    sql是测试从业者必备的技能之一,基本上也是笔试必考内容. 所以,不要让sql拖了后腿,有些测友一遇到多表关联查询就犯晕,甚至连单表的执行顺序都没搞懂,下面简单介绍下,顺便给一些题供大家练习. 单表执 ...

  4. 攻防世界--神奇的Modbus

    使用Wireshark在数据包中寻找flag flag是明文形式存储wireshark输入modbus过滤并追踪tcp流追踪tcp流 得到结果:ctf{Easy_Modbus} (虽然在wiresha ...

  5. ASPack壳脱壳实验

    实验目的 1.学会使用相关软件工具,手动脱ASPack壳. 2.不要用PEiD查入口,单步跟踪,提高手动找入口能力. 实验内容 手动对文件"ASPack 2.12 - Alexey Solo ...

  6. 案例七:shell实现开机自动播放挂载本地yum仓库程序

    shell实现开机自动挂载本地YUM仓库自动化程序,可以在没有网络的情况下也可以使用yum安装程序. #!/bin/bash #自动搭建yum本地仓库 # cdrom () { mount /dev/ ...

  7. C# 枚举器(enumerator)

    总结: 1.枚举器就像是序列中的"游标"或"书签".可以有多个"书签",移动其中任何一个都可以枚举集合,与其他枚举器互不影响.用来遍历数据结 ...

  8. 【C#表达式树 一】Expressions 命名空间 38个类 2个接口 3个枚举

    注解 抽象类 Expression 提供用于为表达式树建模的类层次结构的根. 此命名空间中派生自的类 Expression (例如 MemberExpression 和 ParameterExpres ...

  9. MyEclipse 启动tomcat本地服务,debug模式,代码一直不同步

    今天写代码遇到一个问题,上午还能正常运行的代码,在eclipse中显示正常,但在游览器中就出现了差异,在网上找了很多方法: 1.add and remove项目,清理tomcat部署目录下的项目,清理 ...

  10. Scrapy(六):Spider

    总结自:Spiders - Scrapy 2.5.0 documentation Spider 1.综述 ①在回调函数Parse及其他自写的回调函数中,必须返回Item对象.Request对象.或前两 ...