49

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 ?

6 Answers 6

61

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
Sign up to request clarification or add additional context in comments.

6 Comments

True except for the reasons. The docs meantion only data write-through for being a bridge between array and collection bases APIs
@sblundy and the docs meantion not only the bride "This method also provides a convenient way to create a fixed-size ist initialized to contain several elements". But I give you a vote up.
which is the mean reason I use it.
I like the "view" explanation.
@Ralph Thanks. I got it wrong early. view.set(0, 42) will also change original[0]. The Arrays.ArraysList contains a private final E[] a reference to the original array
|
13

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.

Comments

7

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.

Comments

6

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.

Comments

2

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 .

1 Comment

No you are not able to add elements to the java.utils.Arrays.ArrayList returned from Arrays.asList(T ... a). Your code snippet constructs a new java.util.ArrayList, which, like you show, supports #add.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.