When we first learned Java, the teacher would say that ArrayList is a very useful variable-length array. You only need to insert and delete it, which is much simpler than the Array Control of C ++,
ArrayList is a typical auto-increment array.
Private int size; // the actual number of elements in the array
Capacity indicates the total size of the array. For example, if arr = new int [5], capacity = 5.
// InitialCapacity is the initial array size, because the disadvantage of the Self-increasing array is that it will consume memory when the array is re-allocated,
Redistribution means that the garbage collection of the original space increases the burden of garbage collection,
Therefore, if the allocation is larger at the beginning, the number of reallocation attempts will be avoided in the future.
For example, if you want to insert 200 elements:
(1) Assign the capacity of 200 elements at the beginning.
(2) allocate one space at the beginning, and allocate one more space if it is not enough. If there is no garbage collection, 1 + 2 + 3 + ...... 200 = 20100 Space
200 and 20100 are quite different.
First, let's review the two most important statements in ArrayList:
Int [] newarr = Arrays. copyOf (int [] old, int newlength );
// Newarr = Arrays. copyOf (oldarr, newlength );
// Copy all content of oldarr to newarr. The length of newarr is newlength.
System. arraycopy (oldarr, oldbegin, newarr, newbegin, length );
// Copy the child array with the length starting from oldbegin in oldarr to the position of newarr starting from newbegin.
For example, oldarr = {1, 2, 3 }. newarr = {5, 6, 7}, oldbegin = 1, newbegin = 1, length = 2. the result is {5, 2, 3 }.
For example, oldarr = {1, 2, 3, 4 }. newarr = {5, 6, 7}, oldbegin = 1, newbegin = 1, length = 3. then, the result is ArrayOutofBoundsException, because the newarr length is not enough.
[Java]
Public ArrayList (int initialCapacity ){
If (initialCapacity <0)
Throw new IllegalArgumentException ("Illegal Capacity:" + initialCapacity );
This. elementData = new Object [initialCapacity];
}
[Java]
// The size of the default value is 10.
Public ArrayList (){
This (10 );
}
// If the current capacity is <minCapacity, the array capacity is increased to minCapacity.
[Java]
Public void ensureCapacity (int minCapacity ){
Int oldCapacity = elementData. length;
If (minCapacity> oldCapacity ){
Object oldData [] = elementData;
Int newCapacity = (oldCapacity * 3)/2 + 1; // newlength = (oldlength * 3)/2 + 1
If (newCapacity <minCapacity)
NewCapacity = minCapacity;
ElementData = Arrays. copyOf (elementData, newCapacity); // this statement is the core statement of the Self-increment array.
}
}
Add Element
[Java]
Public boolean add (E e ){
EnsureCapacity (size + 1); // SET size + 1. If capacity is not enough, reassign
ElementData [size ++] = e;
Return true;
}
Add Multiple Elements
[Java]
Public boolean addAll (Collection <? Extends E> c ){
Object [] a = c. toArray ();
Int numNew = a. length; // size of the newly inserted element
EnsureCapacity (size + numNew); // insert element money first to determine whether the capacity is sufficient
System. arraycopy (a, 0, elementData, size, numNew); // set a [0 ,... a, length-1] Put It In elementData [size ,.... size + numNew-1] Go
Size + = numNew;
Return numNew! = 0;
}
Remove Element
[Java]
Public E remove (int index ){
RangeCheck (index); // check whether the index is in the array field.
E oldValue = (E) elementData [index];
Int numMoved = size-index-1;
If (numMoved> 0)
System. arraycopy (elementData, index + 1, elementData, index, numMoved );
ElementData [-- size] = null; // causes the Garbage Collector to recycle it.
Return oldValue;
}