The difference between list-arraylist, linkedlist and vectors in the Java Foundation

Source: Internet
Author: User

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

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.