Senior java engineer ----- internal data structures of vector, ArrayList, and linklist

Source: Internet
Author: User

Set is a required question in the Java interview. However, in my interview experience, it is difficult to get a satisfactory response. Most of them can only understand the general usage method, but do not know about its internal structure. Incorrect usage will cause a significant reduction in performance. First, we will introduce ArrayList. As the name suggests, the internal data structure is an array of Java code private transient Object [] elementData; private int size; public ArrayList (int initialCapacity) {} when adding elements, if the capacity is insufficient, expand the Java code public void ensureCapacity (int minCapacity) {modCount ++; int oldCapacity = elementData. length; if (minCapacity> oldCapacity) {Object oldData [] = elementData; int newCapacity = (oldCapacity * 3)/2 + 1; if (newCapacity <minCapacity) newCapaci Ty = minCapacity; // minCapacity is usually close to size, so this is a win: elementData = Arrays. copyOf (elementData, newCapacity);} the size of the new array is * 1.5 + 1, add 1 to ensure that the oldCapacity is 1 can be expanded to 2 (similar to the total number of pages = (total number of records + (number of records per page-1)/number of records per page algorithm) when deleting an element, use System. arraycopy moves all the elements after the deletion to one location in the Vector: [java] private void ensureCapacityHelper (int minCapacity) {int oldCapacity = elementData. length; if (minCapacity> oldCapacity) {Object [] oldData = elementData; int newCapacity = (capacityIncrement> 0 )? (OldCapacity + capacityIncrement): (oldCapacity * 2); if (newCapacity <minCapacity) {newCapacity = minCapacity;} elementData = Arrays. copyOf (elementData, newCapacity) ;}} when the array size is increased, the Java code public E remove (int index) {RangeCheck (index); modCount ++ is doubled by default; E oldValue = (E) elementData [index]; int numMoved = size-index-1; if (numMoved> 0) System. arraycopy (elementData, index + 1, elementD Ata, index, numMoved); elementData [-- size] = null; // Let gc do its work return oldValue;} The linked list is implemented through a linked list, inside is a two-way linked List Java code public class collections List <E> extends AbstractSequentialList <E> implements List <E>, Deque <E>, Cloneable, java. io. serializable {private transient Entry <E> header = new Entry <E> (null, null, null); private transient int size = 0 ;} private static class Entry <E> {E element; Entry <E> next; Entry <E> previous;} you only need to modify the next and previous implementations of the nodes before and after the insertion and deletion as follows: type, internal structure, sequential traversal speed, random traversal speed, append cost, insertion cost, deletion cost, memory, ArrayList array, high, low, so: generally, ArrayList is used for sequential traversal. However, when setting the initial size in the constructor, do not insert or delete the ArrayList (except for deleting the end ), if multiple delete/insert operations and random traversal are required, you can create an ArrayList and add the objects with compound conditions to the new ArrayList, instead of frequently operating the original ArrayList, the original ArrayList is often used to delete/Insert the List and traverse the List in sequence. In summary, the Vector, ArrayList, and sorted List in the List of collections are the best choice. What are the similarities and differences ??? First, vector and ArrayList are both linear arrays based on carry ry. vector is thread-safe, while ArrayList is not. The ArrayList dynamically increases the array size when the new size exceeds the range of the original array, the ArrayList will be increased to 1.5 times + 1 of the original array size, and 1 is added to expand the array size to 2 when the array size is 1. the difference between ArrayList and ArrayList is that ArrayList is a linear array, while ArrayList is a two-way linked list. The internal structure of the array is the node element and the forward and backward nodes, so the ArrayList query speed is relatively fast, the insert/delete operation of the shortlist is relatively slow. In addition, the listing list provides methods not provided. The listing list is used to operate the header and the end of the table, and can be used as heap, queue, and bidirectional queue operations.

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.