Transferred from: http://www.cnblogs.com/dongying/p/4013271.html?utm_source=tuicool&utm_medium=referral
ArrayList is commonly used as one of the set, I do not know as Javaner you have not busy time to take a moment to see ArrayList source it. If you look at it, you will feel that in fact ArrayList actually so a matter, right, the following to see ArrayList part of the source bar.
Public classArraylist<e>extendsAbstractlist<e>ImplementsList<e>, Randomaccess, cloneable, java.io.serializable{Private Static Final LongSerialversionuid = 8683452581122892189L; //set ArrayList default capacity Private Static Final intDefault_capacity = 10; //An empty array that defaults to an empty array when the parameterless constructor is called Private Static Finalobject[] Empty_elementdata = {}; //This is the real array that holds the data. Private transientobject[] Elementdata; //the actual number of elements in the ArrayList Private intsize; //constructs the default array size for incoming default capacity settings PublicArrayList (intinitialcapacity) { Super(); if(Initialcapacity < 0) Throw NewIllegalArgumentException ("Illegal capacity:" +initialcapacity); This. Elementdata =NewObject[initialcapacity]; } //parameterless Construction method defaults to an empty array PublicArrayList () {Super(); This. Elementdata =Empty_elementdata; } //construct method to pass in a collection, copy the value inside the collection to ArrayList PublicArrayList (collection<?extendsE>c) {Elementdata=C.toarray (); Size=elementdata.length; //C.toarray might (incorrectly) not return object[] (see 6260652) if(Elementdata.getclass ()! = object[].class) Elementdata= arrays.copyof (elementdata, size, object[].class); } //Here's a look at how ArrayList dynamically expands an array to implement add and remove Public BooleanAdd (e e) {ensurecapacityinternal (size+ 1);//increments modcount!!elementdata[size++] =e; return true; } Public voidAddintindex, E Element) {Rangecheckforadd (index); Ensurecapacityinternal (Size+ 1);//increments modcount!!System.arraycopy (Elementdata, index, Elementdata, index + 1, size-index); Elementdata[index]=element; Size++; } Private voidEnsurecapacityinternal (intmincapacity) { if(Elementdata = =empty_elementdata) {mincapacity=Math.max (default_capacity, mincapacity); } ensureexplicitcapacity (mincapacity); } Private voidEnsureexplicitcapacity (intmincapacity) {Modcount++; //exceeds the length that the array can accommodate and needs to be dynamically extended if(Mincapacity-elementdata.length > 0) grow (mincapacity); } //This is the essence of the dynamic expansion, see this method, ArrayList instantly be called back to the prototype Private voidGrowintmincapacity) { intOldcapacity =elementdata.length; //sets the capacity of the new array to expand to 1.5 times times the original array intNewcapacity = oldcapacity + (oldcapacity >> 1); //let's see if the new array has enough capacity to create a new array directly using this length.//not enough, set the length of the array to the length you want. if(Newcapacity-mincapacity < 0) newcapacity=mincapacity; //Judging if there's no more than the maximum limit if(Newcapacity-max_array_size > 0) newcapacity=hugecapacity (mincapacity); //Copy the value of the original array to the new array, ArrayList reference to the new array//The new array is created here, and if the amount of data is large, duplicate the created array, then it will still affect the efficiency,//It is therefore encouraged to specify the default Capaticy size at the appropriate time by constructing the methodElementdata =arrays.copyof (Elementdata, newcapacity); } Private Static intHugecapacity (intmincapacity) { if(Mincapacity < 0)//Overflow Throw NewOutOfMemoryError (); return(Mincapacity > Max_array_size)?Integer.MAX_VALUE:MAX_ARRAY_SIZE; } }
In fact, the essence of ArrayList is the array, ArrayList is the dynamic expansion of the logarithm group, its add, get, remove and so on is the operation of the array. At this point, do you still need to remember the characteristics of the ArrayList described in the book? If you have to remember, it will be a weak blow! Some of the features of ArrayList are derived from arrays: orderly, repeatable elements, slow insertions, fast indexing, and so on. A series of God horse so-called attributes, you understand? There is a feeling that the wood has an epiphany
"Turn" ArrayList in fact, the source of the same thing