Java record-46-ArrayList source code analysis

Source: Internet
Author: User

Java record-46-ArrayList source code analysis
Collections are often used in programming, and ArrayList is the most commonly used in collections. The object is still referenced in the collection, not the object itself. It is the same as the array. When we learned arrays and learned collections, we found the collection was amazing. The size of the array needs to be declared during definition, while the ArrayList does not need to care about the size. It can be used after definition. View the source code of ArrayList. We can find that the underlying layer of ArrayList stores Elements Using arrays. ArrayList is not magical. The underlying implementation uses an Object array Object [] to store elements. Because it is an Object array, it can only store objects and any objects. Arrays store object references, so ArrayList also stores object references. Two constructor functions of ArrayList: we usually use the constructor without parameters when using ArrayList, or specify the constructor of a capacity parameter, the underlying implementation of the constructor is that constructor without parameters will call constructor with parameters, but constructor without parameters will pass a 10 parameter by default, A constructor with parameters declares an array of objects with a length of 10.

   /**     * The array buffer into which the elements of the ArrayList are stored.     * The capacity of the ArrayList is the length of this array buffer.     */   private transient Object[] elementData;       public ArrayList(int initialCapacity) {        super();        if (initialCapacity < 0)            throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity);        this.elementData = new Object[initialCapacity];    }    /**     * Constructs an empty list with an initial capacity of ten.     */    public ArrayList() {        this(10);    }

 

The following is the add method that we often use. This method first checks whether the length of the underlying array is greater than the required length and puts the elements in the underlying array.
    /**     * Appends the specified element to the end of this list.     *     * @param e element to be appended to this list     * @return <tt>true</tt> (as specified by {@link Collection#add})     */    public boolean add(E e) {        ensureCapacity(size + 1);  // Increments modCount!!        elementData[size++] = e;        return true;    }

 

The size () method of ArrayList returns the size of the Set, which is actually the length of the underlying array. An important method in ArraList (the underlying layer of ArrayList is implemented using arrays. why can it add elements infinitely ?) The following method ensureCapacity is implemented. minCapacity is the position of the element to be appended. It is compared with the size of the underlying array. If it is smaller than the length of the underlying array, nothing is done; when the length of the underlying array is greater than the length of the array, it does some things: resizing the size of the original array, how large is it, the size of the original array is 3/2 + 1, and then using Arrays. the copyOf method copies the elements of the original array to the new array of the specified size, so as to resize the array.
   /**     * Increases the capacity of this <tt>ArrayList</tt> instance, if     * necessary, to ensure that it can hold at least the number of elements     * specified by the minimum capacity argument.     *     * @param   minCapacity   the desired minimum capacity     */    public void ensureCapacity(int minCapacity) {        modCount++;        int oldCapacity = elementData.length;        if (minCapacity > oldCapacity) {            Object oldData[] = elementData;            int newCapacity = (oldCapacity * 3)/2 + 1;            if (newCapacity < minCapacity)                newCapacity = minCapacity;            // minCapacity is usually close to size, so this is a win:            elementData = Arrays.copyOf(elementData, newCapacity);        }    }

 

Previous: http://www.bkjia.com/kf/201510/447439.html

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.