PS: This blog is mainly about the underlying source code of the JDK. Rather than write your own code.
Excuse me ArrayList , LinkedList , Vector the Difference
① ArrayList The underlying is actually implemented with an array (and the type of the array Object type of)
② hypothesis Jdk6 , using array.copyof () method to generate a new array, assuming that the Jdk5 , the use is system.arraycopy () method (when the amount of data added is greater than the length of the array)
③ List List = Newarraylist () , the underlying generates a length of Ten array to hold the object
④ ArrayList , Vector the underlying is implemented using arrays
⑤ for ArrayList . Methods are not synchronous, for vectors. Most of the public methods are synchronous
⑥ LinkedList The use of two-way circular linked list
⑦ for ArrayList , the query is very fast, adding and removing (not the last node) operation is very slow (essentially determined by the characteristics of the array)
⑧ for LinkedList , queries are slow, and add and delete operations are fast (essentially determined by a two-way circular list)
1. Default construction method code for ArrayList(contains related code):
Private transient Object elementdata[];p ublic ArrayList (int i) {if (I < 0) {throw new illegalargumentexception (new STR Ingbuilder ()). Append ("Illegal capacity:"). Append (i). toString ());} else {elementdata = new Object[i];return;}} Public ArrayList () {this (10);}
2. Linklist Default construction method (contains related code)
Public LinkedList () {header = new Entry (null, NULL, NULL); size = 0;header.next = Header.previous = header;} private static class Entry {Object element; Entry Next; Entry previous; Entry (Object obj, Entry entry1, Entry entry2) {element = Obj;next = Entry1;previous = Entry2;}} Private transient Entry header;private transient int size;
3, vector default construction method (including related code)
Public Vector (int i, int j) {if (I < 0) {throw new IllegalArgumentException (New StringBuilder ()). Append ("Illegal Capac ity: "). Append (i). toString ()); else {elementdata = new object[i];capacityincrement = J;return;}} Public Vector (int i) {This (I, 0);} Public Vector () {this (10);} Protected Object elementdata[];p rotected int capacityincrement;
The difference between list-arraylist, linkedlist and vectors in the Java Foundation