Java Fundamentals, collections, Arraylist, source parsing (Basic)

Source: Internet
Author: User

ArrayList

    • What is that, a definition?

This is a dynamic array, which provides the dynamic addition and reduction of elements, implements the list interface (List implementation collection, so also implements the collection interface ) flexibility to set the size of the array and other benefits

  

    • How the interior is implemented
1     /**2 * The array buffer into which the elements of the ArrayList is stored.3 * The capacity of the ArrayList is the length of this array buffer.4      */5     Private transientobject[] elementdata;//internal is an array6 7     /**8 * The size of the ArrayList (the number of elements it contains).9      *Ten      * @serial One      */ A     Private int size;//define Sizes -  -     /** the * Constructs an empty list with the specified initial capacity. -      * -      * @paraminitialcapacity The initial capacity of the list -      * @throwsIllegalArgumentException If the specified initial capacity + * is negative -      */ +      PublicArrayList (intinitialcapacity) {//initialization with parameters A         Super(); at         if(Initialcapacity < 0) -             Throw NewIllegalArgumentException ("Illegal capacity:" + - initialcapacity); -          This. Elementdata =Newobject[initialcapacity]; -     } -  in     /** - * Constructs an empty list with an initial capacity of ten. to      */ +      PublicArrayList () {//default initialization, size is ten -          This(Ten); the}

    • Add what to do
1    Public BooleanAdd (e e) {2Ensurecapacityinternal (size + 1);//make length judgments and modifications (place checks and expansions in one method call, code reading high)3elementdata[size++] =e;4         return true;5     }6     7     Private voidEnsurecapacityinternal (intmincapacity) {8modcount++;9         //overflow-conscious CodeTen         if(Mincapacity-elementdata.length > 0)//To Judge One grow (mincapacity); A     } -  -     Private voidGrowintmincapacity) { the         //overflow-conscious Code -         intOldcapacity =elementdata.length; -         intNewcapacity = oldcapacity + (oldcapacity >> 1);//Enlarge the original One-second -         if(Newcapacity-mincapacity < 0)//min. length +Newcapacity =mincapacity; -         if(Newcapacity-max_array_size > 0)//Maximum length +Newcapacity =hugecapacity (mincapacity); A         //mincapacity is usually close to size, so this is a win: atElementdata =arrays.copyof (Elementdata, newcapacity); -}
    • Add the important one, the expansion,--This is why the actual application to define the size of the array,
1    //the source code of the array2  Public Static<T> t[] CopyOf (t[] Original,intnewlength) {3         return(t[]) copyOf (original, Newlength, Original.getclass ());4     }5 6      Public Static<T,U> t[] CopyOf (u[] Original,intNewlength, class<?extendsT[]>NewType) {// need to copy the original array 7t[] Copy = ((object) NewType = = (object) object[].class)8? (t[])NewObject[newlength]9 : (t[]) array.newinstance (Newtype.getcomponenttype (), newlength);TenSystem.arraycopy (original, 0, copy, 0, One math.min (Original.length, newlength)); A         returncopy; -}
    • Make location additions
1      Public voidAddintindex, E Element) {2 Rangecheckforadd (index);3 4Ensurecapacityinternal (size + 1);//increments modcount!!5System.arraycopy (Elementdata, index, Elementdata, index + 1,6Size-index);//Copy only objects after the current position 7Elementdata[index] =element;8size++;9}
    • Delete Object
 public  E Remove (int   index) {Rangecheck (index);        Modcount  ++;        E oldValue  = Elementdata (index);         int  nummoved = Size-index-1;  if  (nummoved > 0 +1, Elementdata, index, nummoved);// After setting the position of the object, move forward one  elementdata[--size] = null ; //         Let the GC do it work  return      OldValue; }
    • How to traverse it?

Array, traversed by the array's foot tag;

For each implementation of iterator, you can also use iterators

  

Use the change Note:

1, initialize the best definition of the length used to avoid arrarlist internal copy operation;

2, if deleted, as far as possible to remove from the back

3, not thread-safe, all the code is not synchronized,

If necessary, using vectors, the difference between the two is 3 points (the other is basically the same):

Thread-Safe,

More Methods IndexOf,

Expansion of the hit size is: 1 time times

int newcapacity = oldcapacity + ((capacityincrement > 0)?
capacityincrement:oldcapacity);

  

Java Fundamentals, collections, Arraylist, source parsing (Basic)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.