Java Collection Class-arraylist analysis

Source: Internet
Author: User

ArrayList features can dynamically expand non-thread-safe support for serialization of thread-safe list collections.synchronizedlist, copyonwritearraylist member variables private static final int default_capacity = 10;  Initial size transient object[] elementdata; The array of storage elements private int size; Container Size Basic Method add
 Public Boolean Add (e e) {        + 1);  // increments modcount!!        elementdata[size++] = e;         return true ;}
private void ensurecapacityinternal (int mincapacity) {
if (Elementdata = = Defaultcapacity_empty_elementdata) {
mincapacity = Math.max (default_capacity, mincapacity);
}

Ensureexplicitcapacity (mincapacity);
}

private void ensureexplicitcapacity (int mincapacity) {
modcount++;

Overflow-conscious Code
if (Mincapacity-elementdata.length > 0)
Grow (mincapacity);
}

private void Grow (int mincapacity) {
Overflow-conscious Code
int oldcapacity = Elementdata.length;
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);
}

Delete Remove change set check get ArrayList expansion principle

If the array capacity does not need to be expanded after the new element is added, execute the Grow method

The default expansion is 1.5 times times the previous, and expands directly to the size after adding the current element if the capacity is insufficient

Then use arrays.copyof to copy the array.

Related classes and Methods arrays.copyof () System.arraycopy ()

public static native void Arraycopy (

Object SRC,
int Srcpos,
Object dest,
int Destpos,
int length

);

Java Collection Class-arraylist 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.