简体   繁体   中英

Why does Arrays.asList() return its own ArrayList implementation

I recently found out that there are actually 2 different ArrayList implementations in Java (better late than never I guess...).

So I was wondering why does Arrays.asList(T... a) need to return a list which can not be resized ? If they needed an unmodifiable list why add the set(int index, E element) method then ?

So my general question is why not return the java.util.ArrayList from the Arrays.asList(T... a) method ?

Also what do you gain with the java.util.Arrays.ArrayList implementation ?

You asked:

Also what do you gain with the java.util.Arrays.ArrayList implementation ?

It is because the Arrays$ArrayList returned by Arrays.asList is just a view on the original array. So when the original array is changed then the view is changed too.

If one would use an real ArrayList then the elements will be copied, and a change on the orignal array would not infuence the ArrayList.

The reasons to do this are quite simple:

  • performance: no need to copy anyting
  • memory efficent: no second array is needed

The javadoc says that asList returns "a fixed-size list backed by the specified array". If you want to resize the array, you have to create a new one and copy the old data. Than the list won't be backed by the same array instance. The stated goal is "This method acts as bridge between array-based and collection-based APIs." and so write-through to the underlying array is a design requirement.

They are two different classes with different behaviours.

The list returned when you called Arrays.asList is a thin wrapper over the array, not a copy. The list returned is fixed size: attempting to call add will throw an UnsupportedOperationException exception.

The java.util.ArrayList on the other hand keeps its own internal copy of the data and is variable sized.

Arrays.asList需要返回一个无法调整大小的列表 - 因为底层数组不能调整大小 - 但这是可修改的 - 因为允许分配给底层数组中的元素。

actually you are able to add elements to the ArrayList with add. method like this :

List<String> l2= new ArrayList<String>(Arrays.asList(array1));
l2.add("blueCheese");

In my opinion you use it to get the features of a List but applying them to an Array .

Two comments:

1, Attempting to shrink the returned array by calling the remove() method of List interface will throw an UnsupportedOperationException. This is because the inner ArrayList class inside of Arrays class extends AbstractList, and the remove() method in AbstractList throws UnsupportedException.

Thus once the List is returned, you can overstore existing elements EITHER in the array OR in the returned List, BUT you are NOT permitted to grow the array or shrink the array.

  1. In response to:

    actually you are able to add elements to the ArrayList with add. method like this : List l2= new ArrayList(Arrays.asList(array1)); l2.add("blueCheese");

The l2 is an independent copy of the list, so List l2 is now decoupled from the original array&List. So blueCheese in in l2, but not in the original array/List that were backed up from each other.

-dbednar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM