Java Collection source code-arraylist Parsing

Source: Internet
Author: User

Arraylist can be seen as a packaging of an array to implement a variable array.
1. Basic fields of arraylist

// You may be wondering why the array is marked as transient.
// This array cannot be serialized during sample serialization. Therefore, you can only manually serialize and save the arraylist
// Element value. Although all objects in the array are stored as references, during serialization, the real object order will also be
// Columnized and saved. However, if the array is not directly modified by transient,
// The null space in elementdata will also be serialized. One sentence: We serialize the order
// List the real elements in the arraylist, instead of the array elementdata.
Private transient object [] elementdata;

// Number of arraylist Elements
Private int size;

// This field actually belongs to the abstractlist class, but the arraylist inherits it.
// When the structure of the arraylist changes, this field is increased by 1, for example, add or remove.
// Add an element to the method.
Protected transient int modcount = 0;

2. arraylist Constructor
Public arraylist (INT initialcapacity ){
Super ();
If (initialcapacity <0)
Throw new illegalargumentexception ("illegal capacity:" +
Initialcapacity );
This. elementdata = new object [initialcapacity];
}
In fact, it is to create an Array Based on the specified size initialcapacity.
Public arraylist (){
This (10 );
}
It can be seen that the length of the array of the created arraylist object is 10 by default.

3. Add Method
Public Boolean add (E ){
Ensurecapacity (size + 1); // increments modcount !!
Elementdata [size ++] = E;
Return true;
}
When adding an element, the first step is to check whether the array space is large enough. If it is not big enough, it is extended. That is, the method of getting a variable-length array is to create another array with a larger space, copy the elements in the original array. In the second step, save the elements directly to the array.
Public void ensurecapacity (INT mincapacity ){
Modcount ++;
Int oldcapacity = elementdata. length;
If (mincapacity> oldcapacity ){
Object olddata [] = elementdata;
Int newcapacity = (oldcapacity * 3)/2 + 1 ;//How internal containers are extended


If (newcapacity <mincapacity)
Newcapacity = mincapacity;
// Mincapacity is usually close to size, so this is a win:
Elementdata = arrays. copyof (elementdata, newcapacity );
}
}
As you can see, if the new array is 1.5 times the original array during expansion, why is it 1.5 times? In fact, this is a result of considering the balance between space and time. Is an experience value. In fact, some practices are extended to twice the original.

For the analysis of the add method, if the array is not extended, adding an element is constant time. If the extended array is, it is O (n), but in general, the average time is a constant. For more information, see the data structure book.
Therefore, inserting elements at the end of the arraylist is faster.
.

When an element is inserted at a specified position in the arraylist, the average time is O (n ).
Because the elements after the inserted element need to be moved.
Public void add (INT index, e element ){
If (index> size | index <0)
Throw new indexoutofboundsexception (
"Index:" + index + ", size:" + size );

Ensurecapacity (size + 1); // increments modcount !!
System. arraycopy

(Elementdata, index, elementdata, index + 1,
Size-index );
Elementdata [Index] = element;
Size ++;
}
4. Remove Method
This method is also relatively simple, and the average time is O (n)
Public E remove (INT index ){
Rangecheck (INDEX );

Modcount ++;
E oldvalue = (e) elementdata [Index];

Int nummoved = size-index-1;
If (nummoved> 0)
System. arraycopy (elementdata, index + 1, elementdata, index,
Nummoved );
Elementdata [-- size] = NULL; // Let GC do its work

Return oldvalue;
}
5. Get Method
Directly obtain the element based on the index value. The time is a constant.
Public e get (INT index ){
Rangecheck (INDEX );

Return (e) elementdata [Index];
}
6. Contains Method
Public Boolean contains (Object O ){
Return indexof (o)> = 0;
}
Public int indexof (Object O ){
If (O = NULL ){
For (INT I = 0; I <size; I ++)
If (elementdata [I] = NULL)
Return I;
} Else {
For (INT I = 0; I <size; I ++)
If (O. Equals (elementdata [I])
Return I;
}
Return-1;
}
We can see that it is actually a sequential search. Whether it is successful or not, the average time and the worst time are O (N)

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.