Array.aslist vs New ArrayList(Array.asList ())
An overview of the
In this article, we will examine
Arrays.asList(array)
and
ArrayList(Arrays.asList(array))
Make some contrast between the differences.
Arrays.asList
First of all, we have a
Arrays.asList
Method does some viewing and explaining. You can click the link above to see the official API.
As you can see from the API documentation, using this method creates a fixed-size List object for the array. This method simply wraps the array once so that the List can be used in the program. No data is copied or created in this wrapper.
Also, we cannot change the length of the newly created List, because adding or removing elements to the List is not allowed.
However, we can modify the elements of the array in the newly created List. It is important to note that if you make changes to the element data in the List, the corresponding Array data is also changed.
For example, if we look at the following code, we first create an array, then wrap the array as a List, and then we modify an element wrapped as a List.
String[] stringArray = new String[]{"A", "B", "C", "D"};
List<String> stringList = Arrays.asList(stringArray);
Copy the code
Now we modify an element of the wrapped List.
stringList.set(0, "E");
assertThat(stringList).containsExactly("E", "B", "C", "D");
assertThat(stringArray).containsExactly("E", "B", "C", "D");
stringList.add("F");
Copy the code
From the output, we can see that after we modify the List, the original Array is also modified.
Now we have exactly the same elements and order in List and Array.
Now let’s try to wrap it up
stringList
Insert a new element into.
stringList.add("F");
Copy the code
Exception thrown:
java.lang.UnsupportedOperationException
at java.base/java.util.AbstractList.add(AbstractList.java:153)
at java.base/java.util.AbstractList.add(AbstractList.java:111)
Copy the code
As you can see from the above code, if you insert or delete elements from the List, the program will throw
Java. Lang. UnsupportedOperationException anomalies.
ArrayList(Arrays.asList(array))
with
Arrays.asList
In the same way, we can also use **
ArrayList<>(Arrays.asList(array))
To create a List from Array. 支那
However, unlike the above method, the List created using this method is a copy of the data from the old Array. The new List is irrelevant to the old Array. Operations on the data in the new List do not affect the data in the old Array.
In other words, the List created using this method can add and remove elements from the List.
String[] stringArray = new String[]{"A", "B", "C", "D"};
List<String> stringList = new ArrayList<>(Arrays.asList(stringArray));
Copy the code
Now we modify an element in the new List.
stringList.set(0, "E");
assertThat(stringList).containsExactly("E", "B", "C", "D");
Copy the code
Now, let’s look at the differences between List and Array data.
assertThat(stringArray).containsExactly("A", "B", "C", "D");
Copy the code
As you can see from the output above, the data in the old Array has not been modified.
You can check out the source code in JDK Source Code.
We can see that in the source code
Arrays.asList
return
ArrayList
Type and range from [
java.util.ArrayList
] (hg.openjdk.java.net/jdk8/jdk8/j…
The main difference is this
The ArrayList returned from Arrays.asList just wraps the old Array and doesn’t implement it
add
and
remove
Methods.
conclusion
The experiments in this article mainly compare the above two methods of converting an array to a List.
The main difference is whether you can add and delete elements to the converted array.