In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Unsupported Operation Exception is what, I believe many inexperienced people are helpless about this, for this reason this article summarizes the causes and solutions of the problem, through this article I hope you can solve this problem.
public class test {
public static void main(String[] args) {
//define a string of 5 characters
String[] strings = new String[5];
strings[0] = "a";
strings[1] = "b";
strings[2] = "c";
strings[3] = "d";
strings[4] = "e";
//Call asList method in Arrays to convert String[] to List
List list = Arrays.asList(strings);
System.out.println("list:"+list.toString());
//add an element to the list
list.add("f");
System.out.println("list:"+list.toString());
}
}
When, when, when
< String >Then add another element f, and the final output is a,b,c,d,e,f.
This logic was perfect, and it was matched with Zu Lan's perfect.
All right, let's cut to the chase. Let's see the results.
Duang, dumbfounded, hey, what is this ah, why is it wrong, this does not conform to logic ah. I am not a vegetable again, nonono, I am a girl who wants to be a big bird.
The following is the official explanation, no trouble.
1
Let's look at it first and then String[] to List
< String >When there is no error, it is correct to output the result. That's when the add operation on List is reported incorrectly. EMMM, what line did you find wrong?
2
Then let's find out why he reported it wrong. Let's see what happens when we convert, as shown below, asList is to receive the value of String[] by creating an ArrayList.
3
Then look at whether ArrayList is the ArrayList we are familiar with, that is, it can be operated by the add, remove method, as shown in the figure below, obviously, this is not the ArrayList class, but a new inner class ArrayList is created in Arrays, and this inner class is inherited from the AbstractList class. The add method in the AbstractList class will throw UnsupportOperationException exception.
4
So someone wants to ask, hey, why can ArrayList, the answer is that ArrayList has done something for us, that is, rewritten the add method.
If you convert String[] to List, you can't add or remove the converted results, because they are not ArrayList we are familiar with, but ArrayList inside Arrays.
After reading the above content, do you know what kind of method Unsupported Operation Exception is? If you still want to learn more skills or want to know more related content, welcome to pay attention to the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.