Characteristics and differences of vector,arraylist,linkedlist

Source: Internet
Author: User
1.Vector Introduction and Features

1. The vector is internally stored in the form of a dynamic array of data.
2, Vector has the characteristics of the array, through the index to support random access, so through random access to the vector elements of the efficiency is very high, but the implementation of insertion, deletion efficiency compared to the ground, the specific reasons behind the analysis.
3, vector to achieve the Abstractlist abstract class, List interface, so it has more abstractlist and list functions, Before we know that Abstractlist has achieved the method of acquiring iterator and listiterator, so vectors only care about the implementation of the method of array operation,
4, Vector implements the Randomaccess interface, this interface is only declared, no method body, vector support random access.
5, Vector implements the Cloneable interface, this interface is only declared, no method body, vector support cloning.
6. Vector implements the serializable interface, which only declares, does not have a method body, represents a vector to support serialization, that is, vectors can be written to the stream in the form of a stream through ObjectOutputStream.
7,vector is thread-safe
the characteristics of 2.ArrayList1, capacity is not fixed, want to put how much (of course, the maximum threshold, but generally not up to) 2, ordered (element output order and input order) 3,non-thread safe4, the insertion of elements may be expanded, the deletion of elements will not reduce capacity, do not use the index element lookup needs to traverse the array, and use equals comparison.Detailed method: Increase:1, because it is an array implementation, the cost of ArrayList will be larger, and will consider whether the need for expansion, first of the original element number on the addition of a mincapacity, with this value and ooject array size comparison (capacity), if this value is large, then need to expand, The size*1.5+1 of the array, if at this time or mincapacity, then the new capacity is represented by mincapacity, and then the array of new capacity is generated, the original element is assigned to the value, if you want to change the expansion strategy, inherit ArrayList, Rewrite the Ensurecapacity method.
2, add (E) put the newly added elements to the end of the array, only need to use the time of the constant.
3. Add (int,e) adds an element at the specified position of the array, first determining whether the array is present and whether the capacity is allowed, and then moving all elements after the specified position to the back one position before inserting the element into the specified position, using the time cost of O (n).
Delete:Deletes are divided into two deletions, deleting objects, and deleting them by location.1. Delete Objects (empty, and non-null, but all need to traverse)1, if the deleted object is null (NULL), first traversal of the array element is empty, if any, will be deleted using the Fastremove method, in particular, the element behind this position all moved forward one bit, the last one left blank, and GC can be recycled.
2, if not NULL, using the Equals method for comparison, found the corresponding elements, the same Faseremove method to delete.
2. Delete by location1.remove (int) Deletes the element at the specified location, which is the advantage of the array, which greatly improves performance by simply determining whether the index is out of bounds, leaving the deletion directly and not needing to traverse. But the element after the element also needs to move all forward one bit.
To get a single object:Get (int), which first determines whether the bounds are crossed, and then simply returns the element, which is also an advantage of the array.
Traverse:Commonly used iterator design patterns, the iterator method returns an iterator implemented by a parent class.
1, the Hasnext method of the iterator is to determine whether the current position is the last position of the array, and the equality is false, otherwise true.
2, the iterator next method is used to return the current element, and pointing the pointer at the next element, it is worth noting that each time you use the next method, you will determine whether the counter modcount of the container that created the iterator gets is not equal to this time, and the inequality indicates that the size of the collection has been modified. If the concurrentmodificationexception exception is thrown, if the equality calls the Get method returns the element.
To determine whether an element exists:
1, first to determine whether the conditional element is empty, is to traverse the collection, there will return true, otherwise false.
2, NOT NULL, then use the Equals method, you need to traverse the collection.
about ArrayList source code detailed, can refer to ArrayList
the characteristics of 3.LinkedListis a doubly linked list, which is better than ArrayList performance in Add and remove, but get and set are particularly slow.
The Evil Fill:
Bidirectional linked list, a kind of linked list. Each data node has two pointers, pointing directly to the predecessor and direct successor. Therefore, we can conveniently access his predecessor nodes and subsequent nodes.
1. Introduction of the linked list:Linked list is a commonly used data structure, the list here only gives a simple introduction, just the basis of the concept.
1. Concept:
If a node contains data values that point to another node, then multiple nodes can be concatenated into a string, accessing the entire sequence of nodes through only one variable, such that the sequence of nodes is called a list (linked list).
2, one-way linked list:
If each node contains only references to subsequent nodes, such a list is called a one-way list.
3, Two-way linked list:
Each linked list node contains two references, one pointing to the predecessor, one to the back-drive node, which is the doubly linked list.
2, the characteristics of LinkedList:1, LinkedList in the form of linked lists to store data, additions and deletions of elements have a high efficiency, low query efficiency, especially random access, efficiency can not bear to look directly,
2, LinkedList inheritance abstractsequentialdlist (its inheritance and abstractlist, so that its subclasses to implement through the index operation Element), so that LinkedList support the use of the index "" additions and deletions operation,
3, LinkedList directly implement the list interface, so that it can store the elements in order and for each element to provide index values,
4, LinkedList directly implement the Deque interface, deque interface inherits the queue, so that it can be used as a two-way linked list of data structure to use, manipulate elements,
5, LinkedList directly implement the Cloneable interface, so that it can copy all the elements
6, in the use of Objectoutputstream/objectinputstream flow, will first speak linkedlist capacity read/write to the stream, and then the element one by one read/write. About LinkedList source code detailed, can refer to LinkedList

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.