When using arrays, sometimes it is necessary to convert them into list form. But during the operation, we will find that arrays and collections have to be discussed in different situations, which complicates our conversion process. In the conversion of arrays, we can use the Arrays.asList() method. Below we will analyze the description and key points of its method, and then bring the instance method of Arrays.asList() conversion.
1. Description
(1) If List is only used for traversal, use Arrays.asList().
(2) If you want to add or delete elements to List, just create a new java.util.ArrayList, and then add elements one by one.
2. Key points to note
(1) This method is suitable for arrays of object data ( String, Integer...)
(2) This method is not recommended for use with arrays of basic data types (byte, short, int, long, float, double, boolean)
(3) This method links the array with the List: when one of them is updated, the other one is automatically updated
(4) Methods such as add(), remove(), clear() are not supported
3.Examples
Pass the "ArrayList" constructed by Arrays.asList() into the construction method of java.util.ArrayList.
Integer[] a = new Integer[]{1, 2, 3}; List list = Arrays.asList(a); ArrayList arrayList = new ArrayList<>(list);
This constructor uses the Arrays.copyOf method, so there will be no relationship between the array inside java.util.ArrayList and the passed in array.
The above is the method for converting Arrays into lists in Java. I believe that after reading this article, you will already be able to classify arrays and collections when converting. After you learn it, try the above example usage as soon as possible.