Java Knowledge Tree Collection ArrayList

Source: Internet
Author: User

Brief description

ArrayList, which implements the List interface, is an ordered set, that is, the order in which the elements are arranged and the order in which the elements are added, we can understand just this sentence by following the example code and the structure diagram.

Example code:

New Arraylist<integer> (
List.add (
6
List.add (
4
List.add (
7
List.add (
6
List.add (
1);

Structure diagram:

From the above structure we can know that the bottom of the ArrayList is implemented by an array, but it differs from the array in that the capacity of ArrayList grows dynamically, which means that the storage element causes insufficient capacity ArrayList will automatically enlarge the capacity of the array. The details of this process are explained in the source analysis phase.

we are creating ArrayList When the initial capacity size is not specified, the initial capacity size defaults to ten, which is an array that can store up to ten elements. (note ①) Of course we can also manually specify the initial capacity size according to the actual storage needs, see the following sample code:

New Arraylist<integer> (5);

keep in mind that even if you specify the initial capacity size, when the storage element causes insufficient capacity, The ArrayList will still be automatically enlarged. So will this expansion have an upper limit? The answer is yes, the specifics will be explained in the source analysis phase.

Note ①: There is no problem with this sentence in the jdk1.6 version, but the internal implementation of ArrayList () in the jdk1.7 version has been improved. The following source code can be seen comparison:

//the realization of jdk1.6 PublicArrayList () { This(10);}//this (10) is a call to another constructor ArrayList (int initialcapacity) PublicArrayList (intinitialcapacity) {    Super(); if(Initialcapacity < 0)        Throw NewIllegalArgumentException ("Illegal capacity:" +initialcapacity);  This. Elementdata =Newobject[initialcapacity];}//This.elementdata is an array of elements to storePrivate transientobject[] Elementdata;//the realization of jdk1.7 PublicArrayList () {Super();  This. Elementdata =Empty_elementdata;}//Empty_elementdata is an already initialized array, but it has a capacity of 0Private Static FinalObject[] Empty_elementdata = {};

We can make a little analysis of jdk1.7 's improvements: when ArrayList () is called, only the array is initialized but no capacity is specified, so the length of the array is 0, perhaps to avoid wasting storage space. If you were to create a ArrayList by using ArrayList () as a constructor of the previous jdk1.6, you would initialize an array with a capacity of 10, and if the ArrayList were not used, the 10-capacity arrays would be wasted. The more ArrayList there are, the more wasted arrays will be. The jdk1.7 approach, while initializing the array, does not specify the capacity, so that even if the created ArrayList is not used, it does not result in wasted storage space. Can we ask a question about the improvement of jdk1.7? by ArrayList () This constructor to create the ArrayList, when did it specify the array capacity? The answer is when the Add (e) or add (int index, e Element) method is invoked, the specifics are explained in the source analysis phase.

The ArrayList implements the randomaccess Interface, which provides a random access function that allows you to quickly access the elements corresponding to the index through the index. So what is this random access process? This problem will also be answered in the next source analysis phase.

Finally, it is important to note that ArrayList is non-thread-safe, which means it is risky to use it in a multithreaded environment. Therefore, it is recommended to use vectors in a multithreaded environment instead.

Source Analysis (based onJdk1.6.0_45)

when you create a ArrayList , we need to call its constructor, we can call ArrayList ()without specifying the initial capacity, and if you explicitly specify thecapacity of the element to be stored, you can call ArrayList (int initialcapacity) to create a ArrayList with the specified capacity .

//This is a constructor for ArrayList, but from an internal implementation it calls the ArrayList (int initialcapacity) constructor. When we do not specify the initial capacity of the array for large hours, the default incoming initialcapacity value is 10, which is an array with a capacity of 10.  PublicArrayList () { This(10);} PublicArrayList (intinitialcapacity) {    Super(); if(Initialcapacity < 0)        Throw NewIllegalArgumentException ("Illegal capacity:" +initialcapacity);  This. Elementdata =Newobject[initialcapacity];}//an array for storing elements that are initialized when the ArrayList constructor is calledPrivate transientObject[] Elementdata;

--------------------not finished, pending update--------------------

Java Knowledge Tree Collection ArrayList

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.