当前位置:网站首页>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.

 Insert picture description here

Ha ha ha , Still too young , If so , What else did I say , There's nothing more to say ( Stand hand ).
 Insert picture description here
Okay , Don't talk much , Let's see the result directly .
 Insert picture description here

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 .

  1. 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 .
  2. 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 .
    Arrays Medium asList Method
  3. 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 .
     Insert picture description here
     Insert picture description here
     Insert picture description here
  4. 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.

原网站

版权声明
本文为[Mr. Liangliang on the top of the head]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060839590915.html