Java ArrayList source record

Source: Internet
Author: User

1. ArrayList implements the Randomaccess interface, the Randomaccess interface is used to mark whether random access is possible

2. Inherits the Abstractlist class, therefore obtains the Modcount, Modcount uses to implement the fast failure mechanism, if the list has the modification, then modcount self-increment

3. ArrayList does not support concurrency, non-thread-safe

4. Support for storing null elements

5. Expansion, each time is 1.5 times times the original size

 Public voidEnsurecapacity (intmincapacity) {        //if Elementdata is not equal to empty_elementdata (because it might be equal at initialization), then Minexpand is 0 or default_capacity//if Elementdata is not empty Minexpand is 0, you can use this to handle the case of a negative mincapacity.//if Elementdata is empty, then it is the default capacity        intMinexpand = (Elementdata! =empty_elementdata)//Any size if real element table? 0//larger than default for empty table. It ' s already supposed to be//At default size.: default_capacity; //If the minimum capacity is greater than Minexpand, then clear expansion        if(Mincapacity >Minexpand)        {ensureexplicitcapacity (mincapacity); }    }    //determined expansion mincapacity    Private voidEnsureexplicitcapacity (intmincapacity) {Modcount++;//number of modifications plus 1//overflow-conscious Code        if(Mincapacity-elementdata.length > 0) grow (mincapacity); }    Private voidGrowintmincapacity) {        //overflow-conscious Code        intOldcapacity =elementdata.length; //newcapacity is 1.5 times times as large as oldcapacity, which can overflow to 1        intNewcapacity = oldcapacity + (oldcapacity >> 1); ....    }

6. LastIndexOf, contains, indexof method for finding, time complexity of O (N), using the object's Equals method to compare
7. The Clone () method is a deep copy, the difference between a so-called deep copy and a shallow copy is that when you copy an internal reference object, a deep copy clones the original object to a copy, the reference to the copied object points to the new object, and the shallow copy simply copies the reference and still points to the original object. The Clone method in Java object is shallow copy

8. Time complexity of Set () and get () O (1)

9. Calling ensurecapacityinternal (int) causes the modcount+1. Add elements in ArrayList call this function, such as add (object) and add (Int,object)

The time complexity of Add (Object) and add (Int,object) is O (N) because the copy movement of the underlying array

The. Remove (int) method also causes Modcount+1 to have a time complexity of O (N), while internally using elementdata[--size] = NULL, to prevent memory leaks so that the GC frees memory

Remove (Object) uses fastremove illegal, fastremove if the deletion element is not found, then Modcount will not change, and vice versa modcount+1, The reason Fastremove is called fast is because system.arraycopy is used for element movement, System.arraycopy is a native method that avoids the subscript check when Java accesses an array.

. Clear method, modcount+1

AddAll (...) will call the Ensurecapacityinternal method, which makes the time complexity of modcount+1, O (N)

RemoveAll (object)//complement and Retainall (object)///intersection can all use the Batchremove method, O (n^2) time complexity, modcount+1.

16. The fast failure mechanism is reflected in the traversal of the container, iterator will save the current container's Modcount during object initialization, and in each traversal check modcount, if the two are inconsistent, then the container is modified during traversal, Throws Concurrentmodificationexception.

   Private classItrImplementsIterator<e> {        intExpectedmodcount =Modcount;  PublicE Next () {checkforcomodification (); inti =cursor;        ...        }  Public voidRemove () {... checkforcomodification ();        ...        } Final voidcheckforcomodification () {if(Modcount! =expectedmodcount)Throw Newconcurrentmodificationexception (); }    }

Java ArrayList source record

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.