当前位置:网站首页>UnsupportedOperationException异常
UnsupportedOperationException异常
2022-07-06 08:40:00 【头顶凉凉先生丶】
我们先来重温一下题目,有什么疑问的可以移步传送门。String[]和List< String >的区别及相互转换
public class test {
public static void main(String[] args) {
//定义一个字符长度为5的字符串
String[] strings = new String[5];
strings[0] = "a";
strings[1] = "b";
strings[2] = "c";
strings[3] = "d";
strings[4] = "e";
//调用Arrays中的asList方法将String[]转化为List<String>
List<String> list = Arrays.asList(strings);
System.out.println("list<String>:"+list.toString());
//为list添加一个元素
list.add("f");
System.out.println("list<String>:"+list.toString());
}
}
当当当,小可爱们是不是跟偶想的一样呢,这没问题啊,这不挺好的吗,先将String[]转换为List< String >,然后再添加一个元素f,那最终输出的结果是a,b,c,d,e,f。
这套逻辑简直完美,此处配上祖蓝的perfect。

呵呵呵,还是太年轻,如果是这样,偶还说什么,这不没话说了吗(摊手)。
好了,话不多说,咱直接看结果。
duang,傻眼了,咦,这是个啥啊,为啥报错啦,这不符合逻辑啊。偶是不是又变菜啦,nonono,我是要成为大鸟的女纸。
下面开始正式讲解了,不闹啦。
- 咱先看再String[]转为List< String >的时候没有报错,是正确的输出了结果。那是在对List进行add操作的时候,报错啦。emmm,在哪行出错找到了。
- 那么下面就找一下,他为什么报错。我们看一下进行转化的时候,都干了啥,如下图,asList是通过新建一个ArrayList来接收String[]的数值。

- 再看ArrayList是不是我们熟悉的那个ArrayList,也就是可以通过add,remove方法进行操作,如下图,很明显,这并不是ArrayList类,而是Arrays里面新建了一个内部类ArrayList,而这个内部类是继承于AbstractList类,AbstractList类里面的add方法是会抛出UnsupportedOperationException异常的。



- 那么有人要问,咦,那为什么ArrayList可以呢,答案就是ArrayList给我们做好了事情,也就是重写了add方法。
综上所论,如果在将String[]转化为List< String >的时候,是不能对转化出来的结果进行add,remove操作的,因为他们并不是我们熟悉的ArrayList,而是Arrays里面的内部类ArrayList。
边栏推荐
- 目标检测——Pytorch 利用mobilenet系列(v1,v2,v3)搭建yolov4目标检测平台
- JVM performance tuning and practical basic theory - Part 1
- China high purity silver nitrate Market Research and investment strategy report (2022 Edition)
- 2022.02.13 - NC004. Print number of loops
- FairGuard游戏加固:游戏出海热潮下,游戏安全面临新挑战
- egg. JS getting started navigation: installation, use and learning
- [cloud native topic -45]:kubesphere cloud Governance - Introduction and overall architecture of enterprise container platform based on kubernetes
- The problem and possible causes of the robot's instantaneous return to the origin of the world coordinate during rviz simulation
- 游戏解包的危害及资源加密的重要性
- VMware 虚拟化集群
猜你喜欢

游戏解包的危害及资源加密的重要性

C language double pointer -- classic question type

MySQL learning record 11jdbcstatement object, SQL injection problem and Preparedstatement object

Trying to use is on a network resource that is unavailable

IoT -- 解读物联网四层架构

sublime text中conda环境中plt.show无法弹出显示图片的问题

ROS compilation calls the third-party dynamic library (xxx.so)

marathon-envs项目环境配置(强化学习模仿参考动作)

同一局域网的手机和电脑相互访问,IIS设置

2022.02.13 - NC001. Reverse linked list
随机推荐
On the day of resignation, jd.com deleted the database and ran away, and the programmer was sentenced
ROS编译 调用第三方动态库(xxx.so)
优秀的软件测试人员,都具备这些能力
The harm of game unpacking and the importance of resource encryption
Deep analysis of C language data storage in memory
Chrome浏览器的crash问题
sublime text没关闭其他运行就使用CTRL+b运行另外的程序问题
What is CSRF (Cross Site Request Forgery)?
Beijing invitation media
Purpose of computer F1-F12
Unified ordering background interface product description Chinese garbled
C语言深度解剖——C语言关键字
Problems in loading and saving pytorch trained models
The problem and possible causes of the robot's instantaneous return to the origin of the world coordinate during rviz simulation
LDAP Application Section (4) Jenkins Access
hutool优雅解析URL链接并获取参数
[2022 广东省赛M] 拉格朗日插值 (多元函数极值 分治NTT)
3. File operation 3-with
深度剖析C语言数据在内存中的存储
View computer devices in LAN