ArrayList is a list based on an array implementation, which can be seen from the source code:
transient // Non-private to simplify nested class access
It can be seen that the interior of ArrayList is given an array to handle.
To find the index of an element from ArrayList, its time complexity is O (n), and its source code is as follows:
Public intindexOf (Object o) {if(O = =NULL) { for(inti = 0; i < size; i++) if(elementdata[i]==NULL) returni; } Else { for(inti = 0; i < size; i++) if(O.equals (Elementdata[i]))returni; } return-1; }
ArrayList supports clone, which is done using arrays.copyof (object[],int):
PublicObject Clone () {Try{ArrayList<?> v = (arraylist<?>)Super. Clone (); V.elementdata=arrays.copyof (elementdata, size); V.modcount= 0; returnv; } Catch(clonenotsupportedexception e) {//This shouldn ' t happen, since we is cloneable Throw NewInternalerror (e); } }
The time complexity of getting an array based on index in ArrayList is O (1), and its source code is as follows:
@SuppressWarnings ("Unchecked") E elementdata (int index) { return ( E) Elementdata[index]; } Public E get (int index) {// See here rangecheck (index); return Elementdata (index); }
Replaces the element at the specified position, and the time complexity is also O (1):
Public E set (int index, e Element) { Rangecheck (index); = Elementdata (index); = element; return oldValue; }
The time complexity of adding an element at the end is also O (1):
Publicboolean Add (e e) { + 1); // increments modcount!! elementdata[size++] = e; return true ; }
It is important to note that the capacity is extensible and the maximum capacity that can be extended is integer.max_value-8:
Private voidEnsurecapacityinternal (intmincapacity) { if(Elementdata = =defaultcapacity_empty_elementdata) {mincapacity=Math.max (default_capacity, mincapacity); } ensureexplicitcapacity (mincapacity); } Private voidEnsureexplicitcapacity (intmincapacity) {Modcount++; //overflow-conscious Code if(Mincapacity-elementdata.length > 0) grow (mincapacity); } Private Static Final intMax_array_size = integer.max_value-8; Private voidGrowintmincapacity) { //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); }
The time complexity that is added to the specified index position is O (n), where you need to move the position and the following elements back together one after the other:
Public void Add (int Index, E Element) { Rangecheckforadd (index); + 1); // increments modcount!! System.arraycopy (Elementdata, index, Elementdata, index + 1, - index); = element; Size+ +; }
Deleting elements of the specified index position time complexity is also O (n), where all elements after this element are moved forward one:
public E Remove (int index) {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 does its work return OldValue; }
The time complexity of deleting an element is also O (n), which shows the element to be detected, deleted, followed by the following element:
Public BooleanRemove (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; } Private voidFastremove (intindex) {Modcount++; intnummoved = size-index-1; if(nummoved > 0) system.arraycopy (elementdata, index+1, Elementdata, Index, nummoved); elementdata[--size] =NULL;//clear to let GC do it work}
Although it is declared that the storage array is not serializable, the ArrayList can be serialized as long as the object being stored is serializable:
Private voidWriteObject (java.io.ObjectOutputStream s)throwsjava.io.ioexception{//Write out element count, and any hidden stuff intExpectedmodcount =Modcount; S.defaultwriteobject (); //Write out size as capacity-behavioural compatibility with clone ()s.writeint (size); //Write elements in the proper order. for(inti=0; i<size; i++) {s.writeobject (elementdata[i]); } if(Modcount! =Expectedmodcount) { Throw Newconcurrentmodificationexception (); } } Private voidReadObject (java.io.ObjectInputStream s)throwsjava.io.IOException, classnotfoundexception {elementdata=Empty_elementdata; //Read in size, and any hidden stuffS.defaultreadobject (); //Read in capacityS.readint ();//ignored if(Size > 0) { //Be-like clone (), allocate array based upon size not capacityensurecapacityinternal (size); Object[] a=Elementdata; //Read in all elements in the proper order. for(inti=0; i<size; i++) {A[i]=S.readobject (); } } }
From the above scenario, ArrayList is not thread-safe and has a significant time complexity advantage when it comes to index lookup and final insertion.
Discussion on Java data structure-arraylist