Grilled a grilled series of common Java collection classes in the development (ArrayList JDK 1.7)

Source: Internet
Author: User

About this series, because the development is mainly used in the Java language, has always wanted to write some of the common Java Development classes (although this is the beginning of the third >_<), all the "grilled a Steak" series. This series will have frames, or other things learned. The article is as concise as possible, with less space to straighten out the relevant knowledge points and use methods. Say less nonsense, next grilled ArrayList this collection class.

first, the data structure

Let's take a look at the location of the ArrayList package, in Java.util.ArrayList (1-1), which is in the JDK util, which shows that it is more commonly used in development. From its name you can see the data structure as an array, and look at the private properties of this class.

Private transient object[] elementdata;//Note the meaning of transient and whether it can be serialized.

From the code above, it can be verified that its internal structure is an array.

Figure 1-1

The inheritance diagram for ArrayList on the graph

Second, common methods

First of all, if you want to manipulate the list, make sure that the class has been instantiated, or that the method parameter passed in is not NULL, otherwise it will cause the NPE exception. You can use the Collectionutils tool to make a null verification.

1, Boolean Add (E E)

2, get (int index)

3, iterator<e> Iterator ()

4. Size ()

Third, the source of the main focus point

Since the underlying data structure is an array, it cannot be changed when it is initialized, and when the Add method is called, it is determined if the array capacity is available, and if it is not satisfied it will be expanded. The main expansion codes are:

Private voidEnsurecapacityinternal (intmincapacity) {    if(Elementdata = =empty_elementdata) {mincapacity=Math.max (default_capacity, mincapacity); } ensureexplicitcapacity (mincapacity);}Private voidEnsureexplicitcapacity (intmincapacity) {Modcount++; //overflow-conscious Code    if(Mincapacity-elementdata.length > 0) grow (mincapacity);}/*** Increases the capacity to ensure so it can hold on least the * number of elements specified by the Minimu     M capacity argument. *     * @parammincapacity the desired minimum capacity*/    Private voidGrowintmincapacity) {        //overflow-conscious Code        intOldcapacity =elementdata.length; intNewcapacity = 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;}

The main logic, when calling the Add method will determine whether it is the initial expansion, if it is the initial setup, then each increase will determine whether the need for expansion, each expansion of the original 1.5 times times.

ArrayList is thread insecure (does not understand what thread safety can be under Google *-*), and errors occur when multiple threads are operating. It is not possible to loop for a for traversal when the side is deleted, which throws a Modexception exception. A list that requires thread safety can go to the Java. Util.concurrent class under the package.

Some methods in the source code often use System.arraycoy This method, this is a native method, belongs to a bottom method.

Hope this article can help you, if you are interested please see the JDK source code. That will increase your skill points.

Grilled a grilled series of common Java collection classes in the development (ArrayList JDK 1.7)

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.