ArrayList is a generic class in Java, using the form of arrays to implement the collection list interface to achieve the role of dynamic arrays, in the case of uncertain data volume, the use of ArrayList is a good choice.
In this demo, only the simple implementation of ArrayList, the purpose is to understand the ArrayList work principle, deepen the understanding of ArrayList.
The secret of ArrayList's ability to implement dynamic arrays is to judge every time an element is added, check if the array is full, and if so, create a new array, twice times the original, and copy the old array into the new array, discarding the old array. This is the key to ArrayList, but it is also the flaw, because this expansion of the operation is to compare the impact of performance, especially when adding many elements at once, the array is extended multiple times. For example, to add 10,000 elements at a time, while the initial size of ArrayList is 10, then 10*2^10=10240, that is, the array has 10 expansion operations. But the developers who developed Java were aware of this, so they provided a construction method that could customize the initial size to avoid multiple expansions of the array.
It is also important to note that ArrayList is thread insecure, and if vector () should be used in multi-threading, it is essentially the same as ArrayList, but with more synchronized keywords.
1 Packagemy;2 3 4 Public classMyarraylist<anytype>5 {6 Private Static Final intDefault_capacity = 10;7 8 Private intthesize;9 Privateanytype[] Theiteme;Ten One Publicmyarraylist () A {Clear ();} - - Public voidClear () the { -thesize = 0; - ensurecapacity (default_capacity); - } + - Public intsize () +{returnthesize;} A at Public BooleanIsEmpty () -{returnThesize = = 0; } - - Public voidtrimtosize () - {ensurecapacity (thesize);} - in PublicAnyType Get (intindex) - { to if(Index < 0 | | index >size ()) + Throw Newarrayindexoutofboundsexception (); - the returnTheiteme[index]; * } $ Panax Notoginseng PublicAnyType Set (AnyType newvalue,intindex) - { the if(Index < 0 | | index >size ()) + Throw Newarrayindexoutofboundsexception (); A theAnyType old =Theiteme[index]; +Theiteme[index] =newvalue; - $ returnOld ; $ } - - Public voidAdd (AnyType x,intindex) the { - if(Theiteme.length = =size ())Wuyi { theEnsurecapacity (Size () + 1); - } Wu - for(inti = size (); i > Index; i--) About { $Theiteme[i] = theiteme[i-1]; - } -Theiteme[index] =x; -thesize++; A } + the Public BooleanAdd (AnyType x) - { $ Add (x, size ()); the return true; the } the the PublicAnyType Remove (intindex) - { in if(Index < 0 | | Index <size ()) the Throw Newarrayindexoutofboundsexception (); theAnyType value =Theiteme[index]; About for(inti = index; I < Size ()-1; i++) the { theTheiteme[i] = theiteme[i + 1]; the } + returnvalue; - } the Bayi PublicJava.util.iterator<anytype>iterator () the{return Newarraylistiterator ();} the - - Private voidEnsurecapacity (intnewcapacity) the { the if(Newcapacity <thesize) the return; the -anytype[] Old =Theiteme; the theTheiteme = (anytype[])Newobject[newcapacity]; the for(inti = 0; I < size (); i++ )94 { theTheiteme[i] =Old[i]; the } the }98 About Private classArraylistiteratorImplementsJava.util.iterator<anytype> - {101 Private intCurrent = 0;102 103 Public BooleanHasnext ()104{returnCurrent <size ();} the 106 PublicAnyType Next ()107 {108 if(!Hasnext ())109 Throw Newjava.util.NoSuchElementException (); the returntheiteme[current++];111 } the 113 Public voidRemove () the{myarraylist. This. Remove (-Current );} the } the}
ArrayList implementation of Java