ArrayList initial capacity problem __arraylist initial capacity problem

Source: Internet
Author: User
Arraylist<string> al = new Arraylist<string> ();
It's a familiar question, but is there any question about the initial capacity of Al?
If the direct print al.size () is of course 1, the problem is not the case.
By viewing the Java.util.ArrayList class file (inside the Jre/rt.jar package)
I'm using the JDK7 version to see the ArrayList constructor as follows:
<pre name= "code" class= "HTML" > Public ArrayList ()
  {this
    ;
  }
<pre name= "code" class= "HTML" > Public  ArrayList (int paramint)
  {
    if (Paramint < 0)
      throw new IllegalArgumentException ("Illegal Capacity:" + paramint);
    This.elementdata = new Object[paramint];
  }
The ArrayList base is implemented with an object-type array, and when the ArrayList object is generated using a constructor with no parameters, an array of type object of length 10 is actually generated at the bottom.
First, ArrayList defines a private, unordered array elementdata that stores the list of ArrayList objects (note that the definition is not initialized):
<pre name= "code" class= "HTML" > Private transient object[] elementdata;
Second, instantiating the Elementdata array with the specified initial capacity (capacity) or converting the specified collection to a reference type array, and if not, the preset initialization capacity is 10 instantiated.
Instantiating a private array and then overwriting the original array via the Copyof method is the key to automating the change of the size of the ArrayList.
ArrayList implements the mechanism for automatically changing size:
<span style= "White-space:pre" >	</span> to implement this mechanism, Java introduced the concept of capacity and size to distinguish length of the array. To ensure that the user adds a new list object, Java sets the minimum capacity (minicapacity), which is typically larger than the number of list objects, so capacity is the length of the underlying array, but it is meaningless for the end user. Size, which stores the number of list objects, is what the end user needs. To prevent user errors from being modified, this property is set to private, but can be obtained by the size () method.
<span style= "White-space:pre" >	</span> below, the automatic size change mechanism is analyzed in three cases, such as the initialization of ArrayList and the addition and deletion of its list objects.
1, initialize the value of capacity and size.
<span style= "White-space:pre" >	</span> from the source code of the ArrayList construction method given above, It's not hard to see that capacity initialization values (initialcapacity) can be specified either directly by the user or by the user specifying the number of objects stored in the collection collection, if not specified, The system defaults to 10 and the size is declared as an int, and the default is 0, the size value equals initialcapacity When the user specifies collection to create the ArrayList.
Add () method.
<pre name= "code" class= "HTML" > Public  Boolean Add (E parame)
  {
    ensurecapacityinternal (this.size + 1) ;
    this.elementdata[(this.size++)] = Parame;
    return true;
  }

  public void Add (int paramint, E parame)
  {
    rangecheckforadd (paramint);
    Ensurecapacityinternal (this.size + 1);
    System.arraycopy (This.elementdata, Paramint, This.elementdata, Paramint + 1, this.size-paramint);
    This.elementdata[paramint] = parame;
    This.size + 1;    Increment size when adding objects
  
Method, Ensurecapacityinternal is used primarily to adjust the capacity and to modify the pointing of the Elementdata array. This involves the invocation of 3 methods, the core of which is the Grow method:
<pre name= "code" class= "html" >private void grow (int paramint)
  {
    int i = this.elementData.length;
    Int J = i + (i >> 1);
    if (J-paramint < 0)
      j = paramint;
    if (j-2147483639 > 0)
      j = hugecapacity (paramint);
    This.elementdata = arrays.copyof (This.elementdata, j);
  }
public void ensurecapacity (int paramint)
  {
    if (Paramint > 0)
      ensurecapacityinternal (paramint);
  }

  private void ensurecapacityinternal (int paramint)
  {
    this.modcount = 1;
    if (Paramint-this.elementdata.length > 0)
      grow (paramint);
  }
Through the above code, we know that Java automatically increase the ArrayList size of the idea is: Add objects to ArrayList, the number of original objects plus 1 if greater than the length of the original underlying array, the appropriate length to create a new copy of the original array, and modify the original array, point to the new array. The original array is automatically discarded (the Java garbage collection mechanism is automatically recycled). Size adds an object to the array, increasing by 1.
To sum up: when the user appends an object to ArrayList, Java always has to calculate the capacity capacity is appropriate, if the capacity is inferior to copy the original array to the length of the specified capacity of the new book Group, and the original array variable to assign value, point to the new array. At the same time, size is increased by 1.
 

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.