当前位置:网站首页>Unsupportedoperationexception exception resolution
Unsupportedoperationexception exception resolution
2022-06-09 10:19:00 【11 brother sun】
Yige In the course of teaching , It often makes people think about how to make themselves become an experienced development God . I think one way to become a great God is , Develop good study habits , For example, record every problem encountered in the learning and development process .
One . bug repeat
see , lately Yige One of my students was writing code , Use Arrays.asList() The utility class wants to convert an array into a collection , Then he wants to add new elements to the set of array transformations , The result is inexplicably thrown
UnsupportedOperationException abnormal , How did this happen ? So I recorded the cause of the exception and the solution process , For your reference to develop their own learning habits .
The core code is as follows :
public static void main(String[] args) {
// 1 Build the original array
String[] strArray = new String[] { "i", "love" };
// 2 adopt Arrays.asList The tool converts an array into List aggregate
List<String> strList = Arrays.asList(strArray);
// 3 Add elements to a collection
strList.add("you");
}
The operation results are as follows :

Two . bug Cause analysis
Just a few lines of code , Such an exception should have occurred , As a student, my mentality is a bit broken . How did the above exception come about ?
We turn on Arrays.asList() Method source code , It can be found from the source code : asList() The return value of the method is a Arrays Inner class --ArrayList, This class does not implement List How to modify a collection ,Arrays.asList() The adapter mode is adopted , Just switched the interface , The data structure in the background is still an array , So we can't directly use asList() Method to add or modify the returned collection !
3、 ... and . Exception resolution
therefore , After analyzing the source code, we can find a solution , That is, you just need to convert the array into a set , adopt ArrayList Construction method of , Constructing a new set can solve this problem perfectly ! The solution code is as follows :
public static void main(String[] args) {
// 1 Build the original array
String[] strArray = new String[] { "i", "love" };
// 2 adopt Arrays.asList The tool converts an array into List aggregate
List<String> strList = Arrays.asList(strArray);
// 3 hold strList Pack into a new one ArrayList
List<String> newStrList = new ArrayList<>(strList);
// 4 Add new elements to the new collection
newStrList.add("you");
}
Now you know how this exception came about , How to solve it ? If you have any questions , You can give a comment to Yige Leave a message .
边栏推荐
- Blazor University (27)路由 —— 检测导航事件
- CSRF跨站请求伪造
- 机器学习笔记 - Transformer/Attention论文解读
- Getting started with cloud based LDAP (Part 2)
- 978. 最长湍流子数组
- 树莓派开发笔记(十五):树莓派4B+从源码编译安装mysql数据库
- Getting started with cloud based LDAP (Part 1)
- 长大后的我们为何贪恋年少?
- Openstack explanation (XI) -- openstack grace service theoretical knowledge
- Construction of vocabulary and sampling -- [torch learning notes]
猜你喜欢

106. 从中序与后序遍历序列构造二叉树

TensorFlow新文档发布:新增CLP、DTensor...最先进的模型已就绪!

【genius_platform软件平台开发】第三十五讲:UDP进行跨网段广播

MSF SSH protocol based information collection

Kubernets chapitre 7: POD Advanced, Controller Advanced, Resource and Dashboard

Openstack explanation (16) -- openstack Nova installation and database configuration

Today in history: PHP is publicly released; IPhone 4 comes out; The birth of the father of the World Wide Web

損失 3 億美元後,IBM 宣布退出俄羅斯!

Machine learning notes - Interpretation of u-net papers

【genius_platform软件平台开发】第三十七讲:网卡混杂模式和原始套接字
随机推荐
106. 从中序与后序遍历序列构造二叉树
Implementing recurrent neural networks from scratch -- [torch learning notes]
Sword finger offer 19 Regular expression matching recursion
Array. Map() shorthand function
MSF tips
[leetcode] [Niuke] brush questions on binary tree
77.5% of the world's websites are using PHP, the "best language in the world"!
1331. 数组序号转换-快速排序加二分查找
MSF practice - harm of ms17-010 vulnerability
【LeetCode】【牛客】二叉树刷题
Extensions attribute of TS generics
106. construct binary tree from middle order and post order traversal sequence
从数据库查询权限信息
Solutions to some medium and high risk vulnerabilities detected by appscan
WPF 实现带明细的环形图表
Openstack explanation (17) -- other configurations of openstack Nova
循环神经网络理论——【torch学习笔记】
明道云上榜2022年中国信创行业办公软件排行榜
Interview question 10.03 Search rotation array
Openstack explanation (12) -- glance installation and Preliminary Configuration