Java Records -46-arraylist Source analysis

Source: Internet
Author: User

The collection is often used in programming, and the ArrayList is the most common in the collection.

The collection is still a reference to the object, not the object itself, as well as the array.

When we learn the array and learn the set, it is amazing to find the collection. The array needs to declare the size at the time of definition, and the ArrayList does not use the tube size, define the future can be used casually.


Looking at the source code of ArrayList, it can be found that the ArrayList is stored with an array of elements. ArrayList is not magical, the underlying implementation is an object array object[] to hold the element, because it is an array of objects, so can only store objects, and can hold any object. The array holds the reference to the object, so ArrayList also holds the object's reference.


Two constructors for ArrayList:

We usually use ArrayList when we use a constructor with no arguments, or a constructor that specifies a parameter of capacity, and its underlying implementation is a constructor that takes parameters without arguments, except that a constructor with no parameters will use the default pass of a 10 parameter. An object array of length 10 is declared in the constructor with parameters.

   /**     * 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];     }  &nbSp; /**     * constructs an empty list with an  initial capacity of ten.     */    public  arraylist ()  {        this ();     }


The following is the Add method that we often use, which first determines whether the length of the underlying array is greater than the length required, allowing the element to be placed in the underlying array.

/** * Appends the specified element to the end of this list. * * @param e element to is appended to the list * @return <tt>true</tt> (as specified by {@link Colle  Ction#add}) */public boolean Add (E e) {ensurecapacity (size + 1);        Increments modcount!!        elementdata[size++] = e;    return true; }

The size of the ArrayList () method returns the amount of the collection, which is actually the length of the underlying array.


A very important method in Arralist (ArrayList is implemented using arrays, why is it possible to add elements infinitely?). )

The following method Ensurecapacity implemented, Mincapacity is to append the position of the element, it and the size of the underlying array is compared, if less than the length of the underlying array do nothing; When the focus is greater than the length of the underlying array, it does something: expand the size of the original array, Enlarge the 3/2+1 of the original array, and then use the Arrays.copyof method to copy the elements of the original array into an array of the new specified size, so that the expansion of the array is realized.

   /**     * 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) &NBSP;{&NBSP;&NBSP;&NBsp;         object olddata[] = elementdata;             int newCapacity =  ( OLDCAPACITY&NBSP;*&NBSP;3)/2 + 1;             if  (newcapacity < mincapacity)                  newCapacity = minCapacity;             // minCapacity is usually close to  size, so this is a win:             elementdata = arrays.copyof (elementdata, newcapacity);         }    }




Java Records -46-arraylist Source analysis

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.