Arrays. asList source code analysis, arrays. aslist source code

Source: Internet
Author: User

Arrays. asList source code analysis, arrays. aslist source code

I used to wonder why the array of Arrays. asList cannot insert new data. After reading the source code, I found that it is an internal Arraylist supported by a final array. The source code and analysis are pasted below.

1. first look at the Arrays Method

We can see that Arrays. aslist returns a new ArrayList <> ();

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

2. Press ctrl and click the ArrayList.

It is found that this ArrayList is not an Arraylist in java. util, but a Private Static class in the Arrays class and inherits AbstractList,

The set and get methods in this class are actually based on the generic array private final E [];

Private static class ArrayList <E> extends actlist <E> // private static class implements RandomAccess, java. io. serializable {private static final long serialVersionUID =-2764017481108945198L; private final E [] a; // This Generic Array supports this static class
ArrayList (E [] array) {// The constructor transmits an array if (array = null) throw new NullPointerException (); a = array;} public E get (int index) {// obtain the return a [index];} public E set (int index, E element) {// Replace the value E oldValue = a [index] in the array; a [index] = element; return oldValue ;}
.........}

3. Wait until we see the add and remove methods. Calm down. Press ctrl and click AbstractList. Since there is no method body in the subclass, it must be implemented in the parent class.

We found that in the parent class, both methods throw an exception (these methods that throw an exception are called optional operations ). Therefore, these two methods cannot be used. But let's see why we have to design it like this. Why don't we directly design an abstract method? Why should we throw an exception.

We need to know that there are many classes that inherit the AbstractList. Some methods have no significance for some classes. For example, we use the array-based ArrayList above, since array-based I cannot change the array size (the array size is fixed), all methods that change the array size will throw the UnsupportedOperationException () exception. I don't need to rewrite these meaningless methods. If they become abstract methods, rewriting will be cumbersome. Besides, you can specify various interfaces in advance to prevent expansion of the inherited class interface, because the inherited class can create its own methods without any constraints. However, if an optional operation is specified in advance, the optional operations will be overwritten, make it easy to use the container class

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

4. You can add or delete a list.

You can pass Arrays. asList () as the Arraylist parameter, as shown below:

    new ArrayList<>(Arrays.asList("1","2"));

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.