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