AbstractSequentialList

AbstractSequentialList

1
public abstract class AbstractSequentialList<E> extends AbstractList<E>

This class is the opposite of the AbstractList class in the sense that it implements the “random access” methods (get(int index), set(int index, E element), add(int index, E element) and remove(int index)) on top of the list’s list iterator, instead of the other way around.
To implement a list the programmer needs only to extend this class and provide implementations for the listIterator and size methods.

  • For an unmodifiable list, the programmer need only implement the list iterator’s hasNext, next, hasPrevious, previous and index methods.
  • For a modifiable list the programmer should additionally implement the list iterator’s set method. For a variable-size list the programmer should additionally implement the list iterator’s remove and add methods.

    need to implement list iterator’s hasNext/Previous() ,next(),previous() ….. set(),add(),remove()

AbstractSequentialList

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public E get(int var1) {
try {
return this.listIterator(var1).next();
} catch (NoSuchElementException var3) {
throw new IndexOutOfBoundsException("Index: " + var1);
}
}

public E set(int var1, E var2) {
try {
ListIterator var3 = this.listIterator(var1);
Object var4 = var3.next();
var3.set(var2);
return var4;
} catch (NoSuchElementException var5) {
throw new IndexOutOfBoundsException("Index: " + var1);
}
}
public Iterator<E> iterator() {
return this.listIterator();
}
public abstract ListIterator<E> listIterator(int var1);

method: get(), set(), add(), remove() —> AbstractList$ListItr

LinkedList

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
public class LinkedList<E>
extends AbstractSequentialList<E>
implements List<E>, Deque<E>, Cloneable, java.io.Serializable{

transient int size = 0;
// Pointer to first node.
// Invariant: (first == null && last == null) ||
// (first.prev == null && first.item != null)
transient Node<E> first;

// Pointer to last node.
// Invariant: (first == null && last == null) ||
// (last.next == null && last.item != null)
transient Node<E> last;

protected transient int modCount = 0;//structurally modified times

///////////////////链表操作///////////////////
// Inserts element e before non-null Node succ.
void linkBefore(E e, Node<E> succ) {
// assert succ != null;
final Node<E> pred = succ.prev;
final Node<E> newNode = new Node<>(pred, e, succ);
succ.prev = newNode;
if (pred == null)
first = newNode;
else
pred.next = newNode;
size++;
modCount++;//....
}

// Unlinks non-null node x.
E unlink(Node<E> x) {
// assert x != null;
final E element = x.item;
final Node<E> next = x.next;
final Node<E> prev = x.prev;

if (prev == null) {
first = next;
} else {
prev.next = next;
x.prev = null;
}

if (next == null) {
last = prev;
} else {
next.prev = prev;
x.next = null;
}

x.item = null;
size--;
modCount++;//。。。。。
return element;
}


////////////////add///remove////////////////
public boolean add(E e) {
linkLast(e);
return true;
}
public boolean remove(Object o) {
if (o == null) {
for (Node<E> x = first; x != null; x = x.next) {
if (x.item == null) {
unlink(x);
return true;
}
}
} else {
for (Node<E> x = first; x != null; x = x.next) {
if (o.equals(x.item)) {
unlink(x);
return true;
}
}
}
return false;
}

private static class Node<E> {
E item;
Node<E> next;
Node<E> prev;

Node(Node<E> prev, E element, Node<E> next) {
this.item = element;
this.next = next;
this.prev = prev;
}
}


public ListIterator<E> listIterator(int index) {
checkPositionIndex(index);
return new ListItr(index);
}

private class ListItr implements ListIterator<E> {
private Node<E> lastReturned;//pointer ---point to current
private Node<E> next;//pointer --- point to next
private int nextIndex;
private int expectedModCount = modCount;

public E next() {
checkForComodification();//here
if (!hasNext())
throw new NoSuchElementException();
//.....
lastReturned = next;
next = next.next;
nextIndex++;
return lastReturned.item;
}
public void remove() {
checkForComodification();//here
if (lastReturned == null)
throw new IllegalStateException();

//....
Node<E> lastNext = lastReturned.next;
unlink(lastReturned);
if (next == lastReturned)
next = lastNext;
else
nextIndex--;
lastReturned = null;
expectedModCount++;//here
}
//....
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
}
}

….

-------------再接再厉更进一步---------------