; Starting nodeLook forward or backward, in which direction the distance is moreNear decisionif (Index for (int i = 0; I e = E.next;} else {for (int i = size; i > index; i--)e = e.previous;}Entry newentry = new Entry (element, E, e.previous);NewEntry.previous.next = Newentry;newEntry.next.previous = Newentry;size++;}
Thread-safe linkedlist and other collectionsIf you want to get a thread-safe linkedlist fro
1 Packagecom.jdk7.chapter4;2 3 Importjava.util.ArrayList;4 Importjava.util.LinkedList;5 Importjava.util.List;6 ImportJava.util.ListIterator;7 ImportJava.util.Stack;8 ImportJava.util.Vector;9 /**Ten * List of forms of collections in Java (sequential allow repetition) One * List of three forms of expression ArrayList, Vector, LinkedList A * where stack extends Vector - * Another: The list of Listiterator can
[].class);
}
This also explains the role of collection in Java-collection-framwork designing collection interfaces rather than directly using interfaces such as List,set.
2, ArrayList capacity allocation mechanism
ArrayList capacity limit: ArrayList capacity is capped, the theory allows the allocation of integer.max_value-8 size capacity. But how much can be allocated is also related to stack settings, you need to set VM parameters
pri
Differences and usages of Java ArrayList, vectors, and linkedlist (RPM)ArrayList and Vector is to take the array format to store data, this array element is larger than the actual data stored in order to grow and insert elements, both allow the direct ordinal index element, but the insertion of data to design to the array element movement and other memory manipulation, so the index data fast insertion data
ArrayList and LinkedList normal for loop traversalFor most Java programmer friends, it is possible to use the most commonly used list is ArrayList, for ArrayList traversal, generally use the following wording:void Main (string[] args) { listnew arraylist( int i = 0; I (int i = 0; i ) System.out.println (Arraylist.get (i));} If you want to use LinkedList i
listiteratorAs we can see, it records the information that points to the last node returned and the next operation node, so we use the following code for LinkedList traversal:ListiteratorBecause the Listiterator interface is implemented, it is also capable of add,set,previous,hasprevious and previousindex relative to iterator. It is also important to note that Listitr has a property Expectedmodcount property that is assigned to
LinkedList, the overhead of adding an element at the end of the list is fixed. For ArrayList, the main point is to add an entry in the internal array, pointing to the element being added, which may occasionally cause the array to be redistributed, whereas for LinkedList, the overhead is uniform, allocating an internal entry object.2. Inserting or deleting an element in the middle of a ArrayList means that
have free space in the queue, and these methods extend this interface.The Queue implementation typically does not allow the insertion of NULL elements, although some implementations, such as LinkedList, do not prohibit the insertion of NULL. Even in a null-enabled implementation, NULL should not be inserted into the queue because Null is also used as a special return value for the poll method, indicating that the queue does not contain elements.The q
caller can arbitrarily modify the returned array.This method serves as a bridge between an array-based API and an collection-based API.39, public toArray(t[] a) returns an array containing all the elements in this list in the appropriate order (from the first element to the last element); Returns the type of the array whose run-time type is the specified array. If the specified array can hold the list, the
, the main point is to add an entry in the internal array, pointing to the element being added, which may occasionally cause the array to be redistributed, whereas for LinkedList, the overhead is uniform, allocating an internal entry object.2. Inserting or deleting an element in the middle of a ArrayList means that the remaining elements in the list will be moved, while the overhead of inserting or deleting an element in the middle of the
Today, when I saw the source code of LinkedList, I met a pit. I study the source, found that LinkedList is a straight-line list structure, but I search for information in Baidu, the source of this part of the analysis, all said LinkedList is a circular linked list structure. I struggled for a long time, thought I understand wrong, and finally found the results in
List overviewList, as its name suggests, is in order. When the list is discussed, it is better to compare it with set, the elements in set are unordered and unique; The following is a class hierarchy chart, from which we can get an overview of the overall architecture of the Java Collection Class ;ArrayList vs LinkedList vs VectorFrom the class hierarchy chart above, we can see that they all implement the l
search process is not small, Therefore, the performance of the LinkedList add (int index,e E) method is not particularly good. Performance is very good if only LinkedList addfrist (e E), AddLast (e E), Offerfrist (E), Offerlast (E), Pollfrist (E), Polllast (e e) are used. Because the search process can be avoided.Similarly, in order to implement the Remove (int index) method,
Differences and usages of Java ArrayList, vectors, and linkedlist (RPM)ArrayList and Vector is to take the array format to store data, this array element is larger than the actual data stored in order to grow and insert elements, both allow the direct ordinal index element, but the insertion of data to design to the array element movement and other memory manipulation, so the index data fast insertion data
LinkedList; ArrayList can be used in cases where the array changes are small and mainly used for querying. Third, Vector Vector is also a variable-length array type similar to ArrayList, whose interior is also used to store data objects in arrays. It is noteworthy that the only difference between vector and ArrayList is that vector is thread-safe, that is, most of its methods contain keyword synchronized, so it is best to use ArrayList instead of ve
) { this. Item = element; this. Next = next; this. prev = prev; } }2.2 Adding operations Public BooleanAdd (e e) {linklast (e); return true; } voidLinklast (e e) {FinalnodeLast ; FinalNodeNewNodeNULL); Last=NewNode; if(L = =NULL) First=NewNode; ElseL.next=NewNode; Size++; Modcount++; }Insert specified position Public voidAddintindex, E Element) { //Check to see if it crossesCheckpositionindex (index); //The last one calls the add if(Ind
The collection package is the most commonly used package in Java, and it is most commonly used with implementation classes for collection and map two interfaces, where collection is used to hold multiple single objects, and map is used to store key-value pairs in key-value form.
There are two types of interfaces commonly used in collection: List and set, the most obvious difference is that the list supports placing duplicate objects, and set does not
List as long as there are two implementation classes (ArrayList and LinkedList), Arrylist is based on the implementation of the array, LinkedList is based on the implementation of the list, the following is the little brother to LinkedList understanding:LinkedList: A collection based on linked list implementation The dual-link list implements {@code list} and
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.