What is the return type of Arrays.asList?

Viewed 8281

I read this post: Difference between Arrays.asList(array) vs new ArrayList<Integer>(Arrays.asList(ia)) in java

and I have a question about it. I look at the line:

List<Integer> list2 = Arrays.asList(ia)

and still there is a line saying:

Of course, some List operations aren't allowed on the wrapper, like adding or removing elements from the list, you can only read or overwrite the elements.

If list2 has a reference of List Interface, I expect it to implement ALL methods included in List interface in Java. https://docs.oracle.com/javase/7/docs/api/java/util/List.html.

add(int index, E element)

and

remove(int index)

show in List interface, so how is it possible that they are not implemented in list2?

I would have expected that list2 is a List; and thus I could call all methods belonging to the List interface?! So why are there exceptions thrown when calling add() or remove()?

5 Answers

It returns java.util.Arrays.ArrayList which is fixed-size list

not java.util.ArrayList

Arrays.asList(ia) method returns an object from a class that extends AbstractList class and AbstractList class implements List interface. The name of this class that extends AbstractList is unfortunately ArrayList (the same name with the public ArrayList that we all know-java.util.ArrayList). But be careful. This class is an inner private static class of class Arrays an instance of which is created and returned by Arrays.asList method. Check the source code of class Arrays here. So the path of this inner private static class is java.util.Arrays.ArrayList and NOT java.util.ArrayList. These are different classes despite having the same name and we can not create java.util.Arrays.ArrayList with new operator because is private. Only as a return from Arrays method like Arrays.asList method.

So as we see in Arrays source code the inner private static ArrayList class extends AbstractList class. AbstractList class is an abstract class that implements List interface. So it has to implement the two overloaded add methods from List interface. As we see in the source code of AbstractList class implements the add(int index,E element) method of List interface so that always returns UnsupportedOperationException.

public void add(int index, E element) {
    throw new UnsupportedOperationException();
}

Why? Be patient. The implementation of second add(E element) calls the previous add(int index,E element) with the first argument as the size of the list

public boolean add(E e) {
    add(size(), e);
    return true;
}

Proud to be here... we continue... If a class that extends AbstractList (like inner ArrayList class of class Array) does not override the two inherited add methods then an UnsupportedOperationException will be returned whenever an add method is called. Checking the source code of inner ArrayList class of class Array we find that there is not overridden method of add(int index, E element)...

So...quod erat demonstrandum

Related