I. Overview
The listlist interface is implemented like the ArrayList interface, except that the ArrayList interface is an array of variable sizes and the listlist interface is a linked List. The implementation of linked list makes the sorted list better than that of ArrayList during insertion and deletion, while random access is inferior to that of ArrayList.
Revoke List implements all optional list operations and allows all elements to include null.
In addition to the List interface, the revoke List class also provides a unified naming method for get, remove, and insert elements at the beginning and end of the List. These operations allow the link list to be used as a stack, queue, or dual-end queue.
This type of Deque interface provides first-in-first-out queue operations for add and poll, as well as other stack and double-end queue operations.
All operations are performed according to the dual-link list. The indexing operation in the list traverses the list from the beginning or end (from the end near the specified index ).
At the same time, this implementation is not synchronous like ArrayList.
(The above is from JDK 6.0 API ).
Ii. source code analysis 2.1. Definition
First, let's first look at the definition of consumer list:
public class LinkedList
extends AbstractSequentialList
implements List
, Deque
, Cloneable, java.io.Serializable
From this code, we can clearly see that the shortlist inherits AbstractSequentialList and implements List, Deque, Cloneable, and Serializable. AbstractSequentialList provides the backbone implementation of the List interface, which minimizes the work required to implement this interface supported by "continuous access" Data Storage (such as the link List, this reduces the complexity of implementing the List interface. Deque is a linear collection that supports inserting and removing elements at both ends and defines the operation of the double-end queue.
2.2 attributes
Two basic attributes, size and header, are provided in the detail list.
private transient Entry
header = new Entry
(null, null, null);private transient int size = 0;
The size indicates the size of the linked list, the header indicates the table header of the linked list, and the Entry indicates the Node object.
Private static class Entry
{E element; // element Node Entry
Next; // The next element Entry
Previous; // The previous element Entry (E element, Entry
Next, Entry
Previous) {this. element = element; this. next = next; this. previous = previous ;}}
The above is the source code of the Entry object, and the Entry is the internal class of the sort list, which defines the stored elements. This is a typical definition of a two-way linked list.
2.3. Constructor
The sorted list method improves the construction of LinkedLis () and sorted list (Collection ). C ).
/*** Construct an empty list. */Public writable list () {header. next = header. previous = header;}/*** constructs a list of elements in the specified collection, which are arranged in the order returned by the collection iterator. */Public Collections list (Collection
C) {this (); addAll (c );}
Construct list () to construct an empty list. There is no element in it, just pointing the first element and the last element of the header node to itself.
Collections list (Collection C): Construct a list of elements in the specified collection, which are arranged in the order returned by the iterator of the collection. The constructor first calls the struct list () to construct an empty list, and then calls the addAll () method to add all elements in the Collection to the list. The source code of addAll () is as follows:
/*** Add all elements in the specified collection to the end of the list. The order is the order in which the collection iterator returns these elements. */Public boolean addAll (Collection
C) {return addAll (size, c);}/*** inserts all elements in the specified collection into the list starting from the specified position. Here, index indicates inserting the index of the first element in the specified collection */public boolean addAll (int index, Collection
C) {// if the inserted position is smaller than 0 or greater than the length of the linked list, an IndexOutOfBoundsException exception is thrown. if (index <0 | index> size) throw new IndexOutOfBoundsException ("Index: "+ index +", Size: "+ size); Object [] a = c. toArray (); int numNew =. length; // Number of inserted elements // if the inserted element is null, false is returned if (numNew = 0) return false; // modCount: defined in AbstractList, indicates the number of times the list is modified from the structure. modCount ++; // obtains the inserted node. If the inserted position is at the size, it is the first node, otherwise, obtain the node Entry at the index position.
Successor = (index = size? Header: entry (index); // The previous node at the insertion position. During the insertion process, you need to modify the next reference of the node: pointing to the inserted node element Entry.
Predecessor = successor. previous; // execute the insert action for (int I = 0; I <numNew; I ++) {// construct a node e, the node insertion action has been executed and the reference pointing to the adjacent node has been modified. // Entry
E = new Entry
(E) a [I], successor, predecessor); // point the next element of the previous node to the current element predecessor. next = e; // modify the previous node of the insert position. The purpose of this operation is to shift the insert position to the right, so that subsequent elements are inserted behind the element, ensure that the order of these elements is predecessor = e;} successor. previous = predecessor; // modify the capacity size + = numNew; return true ;}
The addAll () method involves two methods. One is entry (int index), which is a private method of the sort list, mainly used to find the node element at the index position.
/*** Return the node element at the specified position (if any) */private Entry
Entry (int index) {if (index <0 | index> = size) throw new IndexOutOfBoundsException ("Index:" + index + ", Size:" + size ); // Entry of the header Node
E = header; // determine the direction of traversal if (index <(size> 1) {for (int I = 0; I <= index; I ++) e = e. next;} else {for (int I = size; I> index; I --) e = e. previous;} return e ;}
From the two traversal directions of this method, we can also see that the forward list is a two-way linked list, which is why we need to direct the front and back nodes of the header to ourselves in the constructor.
If you have a little understanding of the data structure and have a problem with the above content, we only need to make it clear that the shortlist is a two-way linked list, and the rest can be solved.
Due to the limited space, the following describes several common methods of listing source code analysis.
2.4 Add Methods
Add (E): add the specified element to the end of the list.
public boolean add(E e) { addBefore(e, header); return true; }
This method calls the addBefore method and returns true directly. For addBefore (), it is the private method of the sort list.
Private Entry
AddBefore (E e, Entry
Entry) {// use the Entry constructor to construct a new node newEntry, Entry
NewEntry = new Entry
(E, entry, entry. previous); // modify the reference of the front and back nodes of newEntry to ensure that the reference relationship of its linked list is correct newEntry. previous. next = newEntry; newEntry. next. previous = newEntry; // capacity + 1 size + +; // number of modifications + 1 modCount + +; return newEntry ;}
In the addBefore method, nothing more than doing this: building a new node newEntry, and then modifying its references.
Else list also provides other adding methods:
Add (int index, E element): insert the specified element at the specified position in this list.
AddAll (Collection C): Add all elements in the specified collection to the end of the list. The order is the order in which the iterator of the specified collection returns these elements.
AddAll (int index, Collection C): inserts all elements in the specified collection into this list starting from the specified position.
AddFirst (E): inserts a specified element into the beginning of the list.
AddLast (E): add the specified element to the end of the list.
2.5. Removal Method
Remove (Object o): removes the specified element that appears for the first time from the List (if any ). The source code of this method is as follows:
public boolean remove(Object o) { if (o==null) { for (Entry
e = header.next; e != header; e = e.next) { if (e.element==null) { remove(e); return true; } } } else { for (Entry
e = header.next; e != header; e = e.next) { if (o.equals(e.element)) { remove(e); return true; } } } return false; }
This method first checks whether the removed element is null, iterates the linked list to find the element node, and finally calls remove (Entry E), remove (Entry E) is a private method, which is the basic method for removing all methods from the listing list, as follows:
Private E remove (Entry
E) {if (e = header) throw new NoSuchElementException (); // retain the removed element: Return E result = e. element; // point the next of the previous node of the node to the later node e. previous. next = e. next; // point the previous of the last node of the node to the front node of the node. // you can remove the node from the linked list in the following two steps: e. next. previous = e. previous; // empty e. next = e. previous = null; e. element = null; size --; modCount ++; return result ;}
Other removal methods:
Clear (): removes all elements from the list.
Remove (): gets and removes the header (the first element) of this list ).
Remove (int index): removes the element at the specified position in this list.
Remove (Objec o): removes the specified element that appears for the first time from the List (if any ).
RemoveFirst (): removes and returns the first element in this list.
RemoveFirstOccurrence (Object o): removes the specified element that appears for the first time from the List (when traversing the list from the header to the end ).
RemoveLast (): removes and returns the last element of this list.
RemoveLastOccurrence (Object o): removes the last specified element from the List (when traversing the list from the header to the end ).
2.5 search methods
There is no good introduction to the source code of the search method. It is nothing more than iteration, comparison, and then return the current value.
Get (int index): returns the element at the specified position in this list.
GetFirst (): returns the first element of this list.
GetLast (): returns the last element of this list.
IndexOf (Object o): returns the index of the specified element that appears for the first time in this list. If this element is not included in this list,-1 is returned.
LastIndexOf (Object o): returns the index of the last specified element in this list. If this element is not included in this list,-1 is returned.
>>>>>> Welcome to my personal site: http://cmsblogs.com/