2

Arrays.asList() is declared as below.

@SafeVarargs
@SuppressWarnings("varargs")
public static <T> List<T> asList(T... a) {
    return new ArrayList<>(a);
}

So, as you know, to initialise a concrete List with an array, you can do this:

String[] strings = new String[] {"hello", "world"};
List<String> list = new ArrayList<>(Arrays.asList(strings));

But what about this?

List<String> list = new ArrayList<>(new String[]{"hello", "world"});

I expected it would works, but didn't.

I understand why. It's because one of the constructors of ArrayList demands Collection class.

Then, what about Arrays.asList() ?

Varargs are compiled as array, so maybe that method would be compiled as below.

public static <T> List<T> asList(T[] a) {
    return new ArrayList<>(a);
}

But it actually returns ArrayList object. Why is this possible?

0

3 Answers 3

6

Indeed, Arrays.asList is implemented as you said, but ArrayList doesn't actually refer to the java.util.ArrayList that we all know and love. It refers to a private inner class in the Arrays class. From the first link, just under the declaration of asList, you will see:

/**
 * @serial include
 */
private static class ArrayList<E> extends AbstractList<E>
    implements RandomAccess, java.io.Serializable
{
    ...

new ArrayList<>(a) is referring to the above class. The above class has a constructor that takes an E[]:

    ArrayList(E[] array) {
        if (array==null)
            throw new NullPointerException();
        a = array;
    }

But the java.util.ArrayList class doesn't have such a constructor, so it is no wonder that you can't do:

List<String> list = new ArrayList<>(new String[]{"hello", "world"});

Another big difference between java.util.ArrayList and java.util.Arrays$ArrayList is that you can add/remove elements to the former, but you can't to the latter. e.g. This throws an exception:

Arrays.asList("a", "b", "c").add("d");
Sign up to request clarification or add additional context in comments.

Comments

2

I absolutely never understood why such a constructor does not exist in java.util.ArrayList to begin with and the decision to name an inner class ArrayList too - is even more interesting. So you are simply looking in the wrong place, nothing more.

They did add such a construct (!= constructor), but only in java-9 and only to immutable collections:

List<String> list = List.of(new String[]{"one"});

2 Comments

Nitpicking, but that's not a constructor, but a factory method. A pretty pointless one, considering there already is one exactly like it - namely Arrays.asList() .
@daniu Note that Arrays.asList() is modifiable - and set/replaceAll affects the content of the original array whereas List.of content is a not modifiable copy of the original array.
0

Arrays.asList() is read only collection.

To new ArrayList(...) you have to pass a collection as an argument.

A collection in java is a List, Set

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.