<pre name="code"><pre name="code">Arrays.asList()
Convert an array into a List object. This method will return an object of type ArrayList. This ArrayList class is not a java.util.ArrayList class, but a static inner class of the Arrays class! If you use this object to add, delete and update the list, you will report an UnsupportedOperationException.
<pre name="code">Test yourself: <span></span>//arrayList<span></span>List list = new ArrayList();<span></span>list.add("yz_b_insert");<span></span>list.add("yz_b_del");<span></span>list.add("yz_b_update");<span></span>list.add("yz_b_see");<span></span>System.out.println(list.contains("yz_b_update")+"=="+list);<span></span>//arrays.asList()<span></span>String dd = "yz_b_insert,yz_b_del,yz_b_update,yz_b_see";<span></span>List list2 = Arrays.asList(dd);<span></span>System.out.println(list2.contains("yz_b_update")+"@@@@@"+list2);Output result:
<pre name="code">arraylist: true==[yz_b_insert, yz_b_del, yz_b_update, yz_b_see]<pre name="code">arrays.asList(): false@@@@@[yz_b_insert,yz_b_del,yz_b_update,yz_b_see]
If you use <pre name="code">contains to determine that there is a certain string in the collection, please note that the <pre name="code">arraylist conversion is not available
I can't test it, I can give some advice if anyone can give me some advice.
Below are the resources found online
<span style="color: rgb(102, 102, 102); font-family: Tahoma;">Why does the List generated by Arrays.asList not be added or deleted, otherwise an UnsupportedOperationException will be generated, and you can get an explanation. </span><br style="color: rgb(102, 102, 102); font-family: Tahoma;" /><br style="color: rgb(102, 102, 102); font-family: Tahoma;" /><span style="color: rgb(102, 102, 102); font-family: Tahoma;">If we want to convert a variable-length or data into a List, and we hope that this List can perform add or remove operations, then what should we do? </span><br style="color: rgb(102, 102, 102); font-family: Tahoma;" /><br style="color: rgb(102, 102, 102); font-family: Tahoma;" /><span style="color: rgb(102, 102, 102); font-family: Tahoma;">We can write a similar method, which directly uses java.util.ArrayList. </span><br style="color: rgb(102, 102, 102); font-family: Tahoma;" /><br style="color: rgb(102, 102, 102); font-family: Tahoma;" /><span style="color: rgb(102, 102, 102); font-family: Tahoma;">比如:</span><br style="color: rgb(102, 102, 102); font-family: Tahoma;" /><pre name="code" style="color: rgb(102, 102, 102); background-color: rgb(255, 255, 255);">import java.util.ArrayList;import java.util.Collections;import java.util.List;public class MyArrays {public static <T> List<T> asList(T... a) {List<T> list = new ArrayList<T>();Collections.addAll(list, a);return list;}}The test code is as follows:
import java.util.ArrayList;import java.util.Arrays;import java.util.List;public class Test {@SuppressWarnings("unchecked")public static void main(String[] args) {List<String> standes = Arrays.asList("Larry", "Moe", "Curly");print(stooges);List<List<String>> seasonsList = Arrays.asList(retrieveSeasonsList());print(seasonsList);/* * Implement an asList method yourself, which can be added and deleted. */List<String> list = MyArrays.asList("Larry", "Moe", "Curly");list.add("Hello");print(list);}private static <T> void print(List<T> list) {System.out.println(list);}private static List<String> retrieveSeasonsList() {List<String> seasonsList = new ArrayList<String>();seasonsList.add("Spring");seasonsList.add("Summer");seasonsList.add("Autumn");seasonsList.add("Winter");return seasonsList;}}Output result:
[Larry, Moe, Curly]
[[Spring, Summer, Autumn, Winter]]
[Larry, Moe, Curly, Hello]
The above is the brief discussion about the differences between Arrays.asList() and ArrayList types brought to you. I hope everyone will support Wulin.com~