用list.add()时报错:

Exception in thread "main" java.lang.UnsupportedOperationExceptionat java.util.AbstractList.add(AbstractList.java:148)at java.util.AbstractList.add(AbstractList.java:108)at com.xxx.xxx.svr.App.main(App.java:37)

重现错误代码:

  public static void main(String[] args)throws Exception {Map<Integer, List<Integer>> itemShopMap = new HashMap<>();itemShopMap.put(1, Arrays.asList(3));List<Integer> shopIdList = itemShopMap.get(1);shopIdList.add(6);}

可改成:

    public static void main(String[] args)throws Exception {Map<Integer, List<Integer>> itemShopMap = new HashMap<>();// itemShopMap.put(1, Arrays.asList(3));List<Integer> num=new ArrayList<>();num.add(9);itemShopMap.put(1,num);List<Integer> shopIdList = itemShopMap.get(1);shopIdList.add(6);}

发生问题的原因如下:

调用Arrays.asList()产生的List中add、remove方法时报异常,这是由于Arrays.asList()返回的是Arrays的内部类ArrayList, 而不是java.util.ArrayList。Arrays的内部类ArrayList和java.util.ArrayList都是继承AbstractList,remove、add等方法在AbstractList中是默认throw UnsupportedOperationException而且不作任何操作。java.util.ArrayList重写这些方法而Arrays的内部类ArrayList没有重写,所以会抛出异常。

list.add时报错:Exception in thread “main“ java.lang.UnsupportedOperationException相关推荐

  1. idea启动springboot时报错Exception in thread “main“ java.lang.UnsupportedClassVersionError:

    idea启动springboot时报错Exception in thread "main" java.lang.UnsupportedClassVersionError:org/s ...

  2. 关于给java数组循环赋值时报错Exception in thread “main“ java.lang.ArrayIndexOutOfBoundsException: 5

    1.检查初始给数组分配的空间是否足够. 2查看循环限制是否超出限制, 假定数组为something, 数组长度为,something.length, 则指定循环限制要<something.len ...

  3. Exception in thread “main“ java.lang.UnsupportedOperationException: Schema for type Any is not suppo

    报错信息 Exception in thread "main" java.lang.UnsupportedOperationException: Schema for type A ...

  4. PI 使用JNative连接报错Exception in thread “main“ java.lang.IllegalStateException: JNative library not load

    使用java调用PiApiTools连接PI数据库报错: Exception in thread "main" java.lang.IllegalStateException: J ...

  5. Exception in thread “main“ java.lang.UnsupportedOperationException Arrays.asList中add报错

    public static void main(String[] args) {List<Integer> integers = Arrays.asList(new Integer[]{1 ...

  6. IDEA运行redis多线程访问报错Exception in thread “main“ java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory

    错误样式: 解决方案: 下载两个JAR包即可,同样的代码我在MyEclipse中运行就不报错,可能是IDEA缺少这两个JAR包吧,而ME本身自带了 托管下载地址:Central Repository: ...

  7. 【亲测有效】如何解决Hadoop运行jar包 报错Exception in thread “main“ java.lang.ClassNotFoundException: /input

    报错解释 字面意思就是我要运行这个jar但是Java无法找到主类 试错过程 你可能会在这里添加主类,但是还是不行.你可能没辙了. 解决办法 <build><finalName> ...

  8. SpringBoot—启动报错Exception in thread “main“ java.lang.ClassNotFoundException

    问题   在微服务启动时,日志排查发现无法启动. 解决方案 查看是否有xxxApplication类. 在pom.xml中查看中的内是否未加xxxApplication类.

  9. springboot启动报错Exception in thread “main“ java.lang.NoClassDefFoundError: io/netty/util/Timer

    原因: 这是缺少netty包的原因. 解决方法: 加入一下依赖: <!-- https://mvnrepository.com/artifact/io.netty/netty-all --> ...

最新文章

  1. WindowsServer2003服务器
  2. 不重启的情况下linux系统安装中文包生效
  3. case zhen语句_switch case语句,switch case用法详解
  4. 小米miui系统已停止服务器,小米两款机型停止 MIUI 更新,明天发布最终体验版固件...
  5. 关于交换机和路由器的区别介绍
  6. python中使用opencv_如何在Python中使用OpenCV Stitcher类?
  7. mock模拟接口测试 vue_Easy Mock以及Vue+Mock.js模拟数据
  8. redis的使用场景和基本数据类型
  9. mac下使用mysql
  10. 初探一个0代码开发—iVX平台
  11. Prometheus - 普罗米修斯 - 日志监控mtail尝试
  12. 圆和长方形周长相等谁的面积大_长方形,正方形和圆的面积相等时,谁的周长最大...
  13. 计算机组成原理浮点运算方法,计算机组成原理第二章 第11讲 浮点运算方法和浮点运算器.ppt...
  14. vnc远程 jetson nano 分辨率问题
  15. 【从零开始vnpy量化投资】三. 手动安装vnpy环境
  16. 关于移动端的touch事件(touchstart, touchmove, touchend,touchcancel)
  17. Linux多线程贝叶斯建树教程,构建系统发育树:贝叶斯法建树
  18. SQL查询语句的使用
  19. These are the first 50 documents matching your search, refine your search to see others
  20. 洛谷 P2404 自然数的拆分问题C语言

热门文章

  1. ansible及ansible-palybook使用(持续更新)
  2. C#如何把List of Object转换成List of T具体类型
  3. 使用mii-tool设置网卡速率
  4. 最长有效括按号长度(利用栈解决java语言)
  5. 浏览器中可以运行Window2000,WebAssembly 是 JavaScript 的末日?
  6. 在koa中想要优雅的发送响应?看这就对了
  7. 古代password
  8. 实现iOS图片等资源文件的热更新化(二):自定义的动态 imageNamed
  9. iOS开发-简单工厂模式
  10. RHCS图形界面建立GFS共享上