Java 2 source code: java. util. ArrayList

Source: Internet
Author: User
Tags addall

Download related source code:
Java. util. ArrayList
Java. util. AbstractList
Java. util. List

ArrayList is a variable-length array implementation of the List interface. All List operations are implemented and null values can be stored. Except for not synchronizing data, ArrayList is basically equivalent to Vector. Almost all methods are synchronized in the Vector, but the ArrayList only synchronizes writeObject and readObject. Other methods such as add (Object) and remove (int) are not synchronized.

1. The storage ArrayList uses an array of objects to store elements.
Private transient Object elementData [];
ArrayList implements the java. io. Serializable interface. The transient indicates that this attribute does not need to be automatically serialized. In the writeObject () method, we will explain in detail why this is necessary.

2. add and remove

  1. Public BooleanAdd (ObjectO ){

  2. EnsureCapacity (size + 1 );// Increments modCount !!

  3. ElementData [size ++] = o;

  4. Return True;

  5. }

Note that the ensureCapacity () method is used to ensure that the length of the elementData array can accommodate a new element. The automatic variable length mechanism is described in detail.

  1. Public ObjectRemove (IntIndex ){

  2. RangeCheck (index );

  3. ModCount ++;

  4. ObjectOldValue = elementData [index];

  5. IntNumMoved = size-index-1;

  6. If(NumMoved> 0)

  7. System. Arraycopy (elementData, index + 1, elementData, index,

  8. NumMoved );

  9. ElementData [-- size] =Null;// Let gc do its work

  10. ReturnOldValue;

  11. }

RangeCheck () is used for border checks. Because ArrayList uses an object array to store elements, you must move the following elements forward when deleting an element. When an element is deleted, the reference of the element in the elementData array is set to null. The Garbage Collector is responsible for the destruction of the object.
The role of modCount is described in "synchronization in iterator ()" below.
Note: arraycopy () is a practical method provided by System when moving forward. In this example, we can see that System. the arraycopy () method can operate on the same array. This method is an native method. If you operate on the same array, it first copies the source part to a temporary array, copy the elements of the temporary array to the target position.

3. when instantiating an ArrayList, you can specify an initial capacity. This capacity is the initial length of the elementData array. If you use:

  1. ArrayList list =NewArrayList ();

The default capacity is 10.

  1. PublicArrayList (){

  2. This(10 );

  3. }

ArrayList provides four add () methods,

  • Public boolean add (Object o)

  • Public void add (int index, Object element)

  • Public boolean addAll (Collection c)

  • Public boolean addAll (int index, Collection c)

In each add () method, an ensureCapacity (int miniCapacity) method is called first. This method ensures that the length of the elementData array is not less than miniCapacity. ArrayList's automatic variable length mechanism is implemented in this method.

  1. Public VoidEnsureCapacity (IntMinCapacity ){

  2. ModCount ++;

  3. IntOldCapacity = elementData.Length;

  4. If(MinCapacity> oldCapacity ){

  5. ObjectOldData [] = elementData;

  6. IntNewCapacity = (oldCapacity * 3)/2 + 1;

  7. If(NewCapacity <minCapacity)

  8. NewCapacity = minCapacity;

  9. ElementData =New Object[NewCapacity];

  10. System. Arraycopy (oldData, 0, elementData, 0, size );

  11. }

  12. }

From the implementation of this method, we can see that each expansion of ArrayList is increased to 1.5 times the original size.
The implementation of each add () method is similar. The following describes the implementation of the add (Object) method:

  1. Public BooleanAdd (ObjectO ){

  2. EnsureCapacity (size + 1 );// Increments modCount !!

  3. ElementData [size ++] = o;

  4. Return True;

  5. }

4. Synchronization in iterator () defines an int type attribute in the parent class AbstractList: modCount, which records structural changes of the ArrayList.

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.