Java Collection (3)--arraylist Source analysis

Source: Internet
Author: User

The default initial capacity is ten, and the underlying object is implemented using an array of objects.

public void ensurecapacity (int mincapacity). Ensure that the minimum capacity of the array is used when adding elements.

Its parent class abstractlist has only one abstract method, abstract public E get(int index);

modcount: Record the number of times the list structure is changed, in fact, change the size will be self-increment, mainly add and Remove,set does not make modcount self-increment.

Affects the operation of Modcount: Add,remove, Ensurecapacityinternal, Fastremove, clear, TrimToSize, RemoveRange (those that change capacity ).

There is also a listiterator() method that returns an iterator.

The code for expansion:

 Public voidEnsurecapacity (intmincapacity) {        if(Mincapacity > 0) ensurecapacityinternal (mincapacity);}Private voidEnsurecapacityinternal (intmincapacity) { Modcount ++; //overflow-conscious Code        if(Mincapacity-elementdata.length > 0) grow (mincapacity);}Private voidGrowintmincapacity) {        //overflow-conscious Code        intOldcapacity =elementdata.length;
1.5 times times the original capacity
int newcapacity = oldcapacity + (oldcapacity >> 1); if(Newcapacity-mincapacity < 0) newcapacity=mincapacity; if(Newcapacity-max_array_size > 0) newcapacity=hugecapacity (mincapacity); //mincapacity is usually close to size, so this is a win:Elementdata =arrays.copyof (Elementdata, newcapacity);}Private Static intHugecapacity (intmincapacity) { if(Mincapacity < 0)//Overflow Throw NewOutOfMemoryError (); return(Mincapacity > Max_array_size)?Integer.MAX_VALUE:MAX_ARRAY_SIZE;}

Look at one more way:

 Public void trimtosize () {       Modcount++       ; int oldcapacity = elementdata.length;        if (Size < oldcapacity) {               = arrays.copyof (elementdata, size);       }  }

The length of the Elementdata is expanded, and thesize tag is the number of elements contained therein. So there will be a small size but elementdata.length a large situation , there will be wasted space . TrimToSize will return a new array to Elementdata, the element content remains unchanged,length is the same as size, saving space

private void Fastremove (int index): does not perform bounds checking and does not return deleted values.

Clear method:AndNot modifiedElementdataOflength, just put all the elementsReset toNull, the size is set to 0.

Sublistthe removerange method that invokes the parent class (ArrayList)。

Java Collection (3)--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.