Java-based set List-ArrayList, sorted List, and Vector

Source: Internet
Author: User

PS: This blog mainly refers to the underlying source code of jdk, rather than writing code by yourself.

What is the difference between ArrayList, struct list, and Vector?

① The underlying layer of ArrayList is actually implemented using arrays (and the Object type of the array)

② If JDK 6 is used, Array is used. the copyOf () method is used to generate a new array. If jdk5 is used, the System is used. arraycopy () method (when the added data volume exceeds the length of the array)

③ When List list = newArrayList (), an array with a length of 10 is generated at the underlying layer to store objects.

④ The underlying layers of ArrayList and Vector are implemented using arrays.

⑤ For ArrayList, the methods are not synchronized. For Vector, most public methods are synchronized.

⑥ Using a two-way circular linked list

7. For ArrayList, the query speed is very fast, and the addition and deletion (not the last node) operations are very slow (essentially determined by the features of the array)

The slow query speed is very slow for the slow list, and the addition and deletion operations are very fast (essentially determined by the two-way cyclic linked list)





<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KMaGiQXJyYXlMaXN0tcTErMjPubnU7Le9t6i0 + sLro6iw/MCoz + rotate "brush: java;"> private transient Object elementData []; public ArrayList (int I) {if (I <0) {throw new IllegalArgumentException (new StringBuilder ()). append ("Illegal Capacity :"). append (I ). toString ();} else {elementData = new Object [I]; return ;}} public ArrayList () {this (10 );}
2. Default LinkList Constructor (including 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. Default Vector Constructor (including related code)

public Vector(int i, int j) {if (i < 0) {throw new IllegalArgumentException((new StringBuilder()).append("Illegal Capacity: ").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[];protected int capacityIncrement;



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.