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

后端 未结 6 486
小蘑菇
小蘑菇 2020-12-02 12:55

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 d

相关标签:
6条回答
  • 2020-12-02 13:36

    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 .

    0 讨论(0)
  • 2020-12-02 13:44

    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

    0 讨论(0)
  • 2020-12-02 13:55

    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.

    0 讨论(0)
  • 2020-12-02 13:55

    Arrays.asList needs to return a list that cannot be resized -- because the underlying array cannot be resized -- but that is modifiable -- because assignment to elements in the underlying array is allowed.

    0 讨论(0)
  • 2020-12-02 13:59

    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
    0 讨论(0)
  • 2020-12-02 13:59

    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.

    0 讨论(0)
提交回复
热议问题