ArrayList Java source code analysis purposes, LinkedList, Vector

ArrayList

jdk 7 where the
list = new ArrayList ArrayList (); // Create a bottom length of Object 10 [] array elementData of
List.add (123); // elementData of [0] = new new Integer (123);
...
List .add (11); // add this if the underlying cause elementData array capacity is not enough, then the expansion.
By default, the expansion of 1.5 times the original volume, while the need to copy the original data array into a new array.


Conclusion : It is recommended for use with the development of argument constructor: ArrayList list = new ArrayList (int capacity), because of frequent expansion is very time-consuming.


Change jdk 8 in an ArrayList:
ArrayList List = new new ArrayList (); // underlayer Object [] elementData initialization did not create an array of length 10} is {.

List.add (123); // first call to add ( ), the bottom of the length of the array 10 is not created, and added to the 123 data elementData of [0]
...
subsequent expansion operations of addition and no different jdk 7.
Summary : create objects in jdk7 ArrayList is similar to the single example of a hungry man, and the jdk8 ArrayList of objects
to create a similar singleton lazy style, delayed the creation of the array, saving memory.

 

LinkedList

LinkedList list = new LinkedList (); internal declares Node type first and last property, the default value is null
List.add (123); // Node to the package 123, the Node object is created.

Wherein, Node is defined as: reflecting the doubly-linked list of statement LinkedList
Private static class the Node <E> {
E Item;
the Node <E> Next;
the Node <E> PREV;

Node(Node<E> prev, E element, Node<E> next) {
this.item = element;
this.next = next;
this.prev = prev;
}
}

 

Vector

jdk7 and jdk8 by Vector () constructor creates when objects are created with a length of the bottom 10 of the array.
In terms of expansion, the expansion is the default 2 times the original length of the array.

Guess you like

Origin www.cnblogs.com/mcq1999/p/12113191.html