当前位置:网站首页>Unsupported operation exception
Unsupported operation exception
2022-07-06 08:42:00 【Mr. Liangliang on the top of the head】
Let's review the topic first , If you have any questions, you can move the portal .String[] and List< String > Differences and mutual transformation of
public class test {
public static void main(String[] args) {
// Define a character length of 5 String
String[] strings = new String[5];
strings[0] = "a";
strings[1] = "b";
strings[2] = "c";
strings[3] = "d";
strings[4] = "e";
// call Arrays Medium asList Methods will String[] Turn into List<String>
List<String> list = Arrays.asList(strings);
System.out.println("list<String>:"+list.toString());
// by list Add an element
list.add("f");
System.out.println("list<String>:"+list.toString());
}
}
Dangdang when , Are the little ones the same as I thought , It's no problem , Isn't that great , First the String[] Convert to List< String >, Then add another element f, The final output is a,b,c,d,e,f.
This logic is perfect , Here with Zulan perfect.

Ha ha ha , Still too young , If so , What else did I say , There's nothing more to say ( Stand hand ).
Okay , Don't talk much , Let's see the result directly .
duang, Silly eyes , Why , What is this , Why did you make a mistake , This is illogical . Have I changed my dish again ,nonono, I want to be a big bird's female paper .
Now let's start the formal explanation , Stop it .
- Let's see first and then String[] To List< String > There is no error reported when , Is the correct output of the results . That's right List Conduct add During operation , Wrong report .emmm, In which line did you make a mistake and found .
- Now let's look , Why did he report an error . Let's take a look at the transformation , What did you do , Here's the picture ,asList By creating a new ArrayList To receive String[] The numerical .

- Look again ArrayList Is it the one we are familiar with ArrayList, That is to say, you can go through add,remove Method to operate , Here's the picture , Obviously , This is not ArrayList class , It is Arrays There is a new internal class ArrayList, This inner class inherits from AbstractList class ,AbstractList The inside of the class add The method is to throw UnsupportedOperationException Anomalous .



- Then someone wants to ask , Why , What then? ArrayList Yes. , The answer is ArrayList Have done something for us , That is to rewrite add Method .
To sum up , If in the future String[] Turn into List< String > When , It is impossible to add,remove Operation of the , Because they are not familiar to us ArrayList, It is Arrays Inner class in ArrayList.
边栏推荐
猜你喜欢
随机推荐
gcc动态库fPIC和fpic编译选项差异介绍
JS inheritance method
Detailed explanation of heap sorting
visdom可视化实现与检查介绍
电脑F1-F12用途
[2022 Guangdong saim] Lagrange interpolation (multivariate function extreme value divide and conquer NTT)
LDAP Application Section (4) Jenkins Access
2022.02.13 - 238. Maximum number of "balloons"
电脑清理,删除的系统文件
JVM performance tuning and practical basic theory - Part 1
Rviz仿真时遇到机器人瞬间回到世界坐标原点的问题及可能原因
@Jsonbackreference and @jsonmanagedreference (solve infinite recursion caused by bidirectional references in objects)
Research and investment forecast report of citronellol industry in China (2022 Edition)
[MySQL] log
On the inverse order problem of 01 knapsack problem in one-dimensional state
Function coritization
UnsupportedOperationException异常
Process of obtaining the electronic version of academic qualifications of xuexin.com
如何有效地进行自动化测试?
Screenshot in win10 system, win+prtsc save location







![[MySQL] lock](/img/ce/9f8089da60d9b3a3f92a5e4eebfc13.png)
