list.add时报错:Exception in thread “main“ java.lang.UnsupportedOperationException
用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相关推荐
- idea启动springboot时报错Exception in thread “main“ java.lang.UnsupportedClassVersionError:
idea启动springboot时报错Exception in thread "main" java.lang.UnsupportedClassVersionError:org/s ...
- 关于给java数组循环赋值时报错Exception in thread “main“ java.lang.ArrayIndexOutOfBoundsException: 5
1.检查初始给数组分配的空间是否足够. 2查看循环限制是否超出限制, 假定数组为something, 数组长度为,something.length, 则指定循环限制要<something.len ...
- 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 ...
- PI 使用JNative连接报错Exception in thread “main“ java.lang.IllegalStateException: JNative library not load
使用java调用PiApiTools连接PI数据库报错: Exception in thread "main" java.lang.IllegalStateException: J ...
- Exception in thread “main“ java.lang.UnsupportedOperationException Arrays.asList中add报错
public static void main(String[] args) {List<Integer> integers = Arrays.asList(new Integer[]{1 ...
- IDEA运行redis多线程访问报错Exception in thread “main“ java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
错误样式: 解决方案: 下载两个JAR包即可,同样的代码我在MyEclipse中运行就不报错,可能是IDEA缺少这两个JAR包吧,而ME本身自带了 托管下载地址:Central Repository: ...
- 【亲测有效】如何解决Hadoop运行jar包 报错Exception in thread “main“ java.lang.ClassNotFoundException: /input
报错解释 字面意思就是我要运行这个jar但是Java无法找到主类 试错过程 你可能会在这里添加主类,但是还是不行.你可能没辙了. 解决办法 <build><finalName> ...
- SpringBoot—启动报错Exception in thread “main“ java.lang.ClassNotFoundException
问题 在微服务启动时,日志排查发现无法启动. 解决方案 查看是否有xxxApplication类. 在pom.xml中查看中的内是否未加xxxApplication类.
- springboot启动报错Exception in thread “main“ java.lang.NoClassDefFoundError: io/netty/util/Timer
原因: 这是缺少netty包的原因. 解决方法: 加入一下依赖: <!-- https://mvnrepository.com/artifact/io.netty/netty-all --> ...
最新文章
- WindowsServer2003服务器
- 不重启的情况下linux系统安装中文包生效
- case zhen语句_switch case语句,switch case用法详解
- 小米miui系统已停止服务器,小米两款机型停止 MIUI 更新,明天发布最终体验版固件...
- 关于交换机和路由器的区别介绍
- python中使用opencv_如何在Python中使用OpenCV Stitcher类?
- mock模拟接口测试 vue_Easy Mock以及Vue+Mock.js模拟数据
- redis的使用场景和基本数据类型
- mac下使用mysql
- 初探一个0代码开发—iVX平台
- Prometheus - 普罗米修斯 - 日志监控mtail尝试
- 圆和长方形周长相等谁的面积大_长方形,正方形和圆的面积相等时,谁的周长最大...
- 计算机组成原理浮点运算方法,计算机组成原理第二章 第11讲 浮点运算方法和浮点运算器.ppt...
- vnc远程 jetson nano 分辨率问题
- 【从零开始vnpy量化投资】三. 手动安装vnpy环境
- 关于移动端的touch事件(touchstart, touchmove, touchend,touchcancel)
- Linux多线程贝叶斯建树教程,构建系统发育树:贝叶斯法建树
- SQL查询语句的使用
- These are the first 50 documents matching your search, refine your search to see others
- 洛谷 P2404 自然数的拆分问题C语言