Recently, I took the time to analyze the source code of the asList method of the java Arrays tool class, compiled relevant information on the Internet, and recorded it. I hope it can also help readers!
The Arrays tool class provides a method asList, which can be used to convert a variable-length parameter or array into a List.
The source code is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Problem discovery
According to the description of the above method, let’s write a few examples first:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
Run the above program , output the following content.
[Welcome, to, Java, world]
[Welcome, to, Java, world]
On a whim, I suddenly want to add a string to the created list" Cool~~~", Go one.
1 2 3 |
|
As a result, an UnsupportedOperationException was encountered:
1 2 3 4 |
|
Incredibly, the list generated by new ArrayList<>(a) encountered a problem when calling the add method.
Cause search
Then the question is, what happened? With questions in mind, let’s check out what the ArrayList used in Arrays.asList looks like?
It turns out that the ArrayList class used by the asList method of Arrays is an internally defined class, not the java.util.ArrayList class.
The source code is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
|
It can be seen from the implementation of this internal class ArrayList that it inherits the abstract class java.util. AbstractList
However, by default, the java.util.AbstractList class will directly throw UnsupportedOperationException in the add, set and remove methods. Part of the source code of AbstractList is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
|
It is precisely because the internal class ArrayList of the java.util.Arrays class does not override the add and remove methods, so when When we call its add method, we actually call the add method of the AbstractList class, and the result is that an UnsupportedOperationException is thrown directly.
Similarly, you will also encounter UnsupportedOperationException when calling the remove method, or calling other methods associated with the add and remove methods (such as addAll).
Example of addAll:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
1 2 3 4 5 |
|
Example of
set:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
It is precisely because the internal class ArrayList of Arrays overrides the set method that the above program can run normally and will no longer throw UnsupportedOperationException.
The results are as follows:
[Welcome, to, Java, world]
[Welcome, to, hello, world]
Usage scenarios
Judging from the above examples and simple analysis, the Arrays tool class provides a method asList, which can be used to convert a variable-length parameter or array into a List.
However, the length of the generated List is fixed; modification operations can be performed (for example, modifying an element at a certain position); operations that affect the length (such as add, remove, etc.) cannot be performed. An UnsupportedOperationException will be thrown.
Arrays.asList is more suitable for scenarios where you already have array data or some elements and need to quickly build a List, which is only used for reading operations without adding or deleting operations.
If you want to quickly obtain a list that can be added, deleted, modified, and checked based on known array data, a relatively simple method is as follows:
Reuse the java.util.ArrayList wrapper layer.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
The results are as follows:
[Welcome, to, Java, world]
[Welcome, to, Java, world, Cool ~~~]
Thank you for reading, I hope it can help everyone, thank you for your support of this site!
For more java source code analysis and detailed explanation of the Arrays.asList method, please pay attention to the PHP Chinese website!