Java Collection ArrayList-thoroughly understand additions and deletions
From the source to see the initialization and additions and deletions to learn ArrayList.
Let's take a look at some of the properties defined by ArrayList:
privatestaticfinalint10;privatestaticfinal Object[] EMPTY_ELEMENTDATA = {};privatestaticfinal Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};transient Object[] elementData; // 保存对象privateint// ArrayList的长度
From here you can see that ArrayList is implemented internally using arrays.
One. Initialization
1. ArrayList ()
Non-parametric constructor generation:
/** * Constructs an empty list with an initial capacity of ten. */ public ArrayList() { this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA; }
You can see that the constructor initializes an empty array. Here is a question, that is, the annotation is clearly constructed a length of 10 array, in fact, this is the addition of the first element when the expansion.
2. ArrayList (int initialcapacity)
constructor of the specified length:
/*** Constructs an empty list with the specified initial capacity. * * @param initialcapacityThe initial capacity of the list * @throws illegalargumentexceptionif the specified initial capacity* is negative */ PublicArrayList (intinitialcapacity) {if(Initialcapacity >0) { This.Elementdata=NewObject[initialcapacity]; }Else if(Initialcapacity = =0) { This.Elementdata= Empty_elementdata; }Else{Throw NewIllegalArgumentException ("Illegal capacity:"+ initialcapacity); } }
This constructor explicitly indicates the length of the array, in fact, if it is less than 10, it will be expanded to an array of length 10 when the first element is added.
Two. Adding elements
1. Add (e E)
/** * Appends the specified element to the end of this list. * * @param e element to be appended to this list * @return <tt>true</tt> (as specified by {@link Collection#add}) * 添加指定的元素到List的末尾 */ publicbooleanadd(E e) { ensureCapacityInternal1); // 扩充数组长度 elementData[size++] = e; // 最后一个赋值为e returntrue; }
So how does it extend the length of the array? The tracking code comes to grow (int mincapacity) method:
/** You can see that it's copying the original array into a new array with an array length of newcapacity using the Arrays.copyof method*/Private void Grow(intmincapacity) {//Overflow-conscious code intOldcapacity = Elementdata.length;intNewcapacity = 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); }
2. Add (int index, E Element)
/** * 在指定的位置添加一个元素 */ publicvoidadd(int index, E element) { rangeCheckForAdd// 检查index是否合法 ensureCapacityInternal1); // 扩充数组长度 System.arraycopy1, size - index); // 通过拷贝返回一个新数组 elementData[index] = element; size++; }
This uses the Arraycopy method of the system class, which uses the following:
System. Arraycopy (object src, int srcpos, object dest, int destpos,int length)
SRC: original array;
Srcpos: The starting position of the source array to be copied;
Dest: Target array;
Destpos: The starting position of the target array placement;
Length: The lengths of the copies
This method can also implement its own replication. The above is a copy of the array using the attribute of its own assignment. The equivalent of moving all the elements behind the index back one bit, inserting the new element into the index position.
Three. Deleting an element
1. Remove (int index)
/* * and add (int index, E Element), using SYSTEM.A Rraycopy makes its own copy, which is equivalent to moving the element after index */ public E remove (int I Ndex) {rangecheck (index); modcount++; E oldValue = elementdata (index); int nummoved = size-index-1 ; if (Nummoved > 0 ) System. arraycopy (Elementdata, Index+1 , Elementdata, Index, nummoved); Elementdata[--size] = null ; //clear to let GC do it work return oldValue; }
2. Remove (Object o)
/** The location of O is first found through the for loop, and then removed by Fastremove (index), the same way as remove (int index) */ Public Boolean Remove(Object o) {if(O = =NULL) { for(intindex =0; index < size; index++)if(Elementdata[index] = =NULL) {Fastremove(index);return true; } }Else{ for(intindex =0; index < size; index++)if(O.equals(Elementdata[index])) {Fastremove(index);return true; } }return false; }
Four. Find elements
/* * 查找元素就比较简单了,直接通过数组的下角标进行返回 */ publicget(int index) { rangeCheck(index); returnelementData(index); }
Five. Modifying elements
/* * 修改元素也比较简单,直接通过数组的下角标进行赋值 */ publicset(int index, E element) { rangeCheck(index); elementData(index); elementData[index] = element; return oldValue; }
Summarize
From the source can be seen, ArrayList in an array way, the performance of the search and modification is better, the increase and deletion of performance is poor.
The ArrayList of Java collection from the Source view